时间:2021-07-01 10:21:17 帮助过:51人阅读
我们要知道,这里主要使用了css3的animation动画属性,首先将进度条设置为一个初始宽度为0,背景色为绿色,高度与容器相同的元素;在通过animation动画属性对其宽度进行过渡,从而实现进度条填充的效果。
我们来看看css3的animation动画属性的相关知识。
animation 属性是一个简写属性,用于设置六个动画属性:
animation-name:规定需要绑定到选择器的 keyframe 名称;
animation-duration:规定完成动画所花费的时间,以秒或毫秒计;
animation-timing-function:规定动画的速度曲线;
animation-delay:规定在动画开始之前的延迟;
animation-iteration-count:规定动画应该播放的次数;
animation-direction:规定是否应该轮流反向播放动画
下面我们来看看具体的实现动态进度条效果的方法。
css+js实现简单的动态进度条效果的代码示例:
html代码:
- <!--外层容器-->
- <div id="wrapper">
- <!--进度条容器-->
- <div id="progressbar">
- <!--用来模仿进度条推进效果的进度条元素-->
- <div id="fill"></div>
- </div>
- </div>
css代码:
- #wrapper{
- position: relative;
- width:200px;
- height:100px;
- border:1px solid darkgray;
- }
- #progressbar{
- position: absolute;
- top:50%;
- left:50%;
- margin-left:-90px;
- margin-top:-10px;
- width:180px;
- height:20px;
- border:1px solid darkgray;
- }
- /*在进度条元素上调用动画*/
- #fill{
- animation: move 2s;
- text-align: center;
- background-color: #6caf00;
- }
- /*实现元素宽度的过渡动画效果*/
- @keyframes move {
- 0%{
- width:0;
- }
- 100%{
- width:100%;
- }
- }
js代码:
- var progressbar={
- init:function(){
- var fill=document.getElementById('fill');
- var count=0;
- //通过间隔定时器实现百分比文字效果,通过计算CSS动画持续时间进行间隔设置
- var timer=setInterval(function(e){
- count++;
- fill.innerHTML=count+'%';
- if(count===100) clearInterval(timer);
- },17);
- }
- };
- progressbar.init();
效果图:
总结:以上就是本篇文章所介绍的css+js实现简单的动态进度条效果的全部内容,大家可以自己动手试试,加深理解,制作不同的进度条效果,希望能对大家的学习有所帮助。
相关推荐:
html5如何实现简单进度条效果?动态进度条的实现
css中clip属性是什么?clip:rect()制作圆形进度条动画
js实现自定义拖动进度条效果
以上就是css+js如何实现简单的动态进度条效果?(代码实例)的详细内容,更多请关注Gxl网其它相关文章!