当前位置:Gxlcms > css > css+js如何实现简单的动态进度条效果?(代码实例)

css+js如何实现简单的动态进度条效果?(代码实例)

时间:2021-07-01 10:21:17 帮助过:51人阅读

css+js如何实现简单的动态进度条?本篇文章就给大家用css+js制作一个简单的动态进度条效果,并将页面动态进度条滚动加载的代码分享给大家,感兴趣的小伙伴可以参考借鉴一下,希望对你们有所帮助。

我们要知道,这里主要使用了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代码:

  1. <!--外层容器-->
  2. <div id="wrapper">
  3. <!--进度条容器-->
  4. <div id="progressbar">
  5. <!--用来模仿进度条推进效果的进度条元素-->
  6. <div id="fill"></div>
  7. </div>
  8. </div>

css代码:

  1. #wrapper{
  2. position: relative;
  3. width:200px;
  4. height:100px;
  5. border:1px solid darkgray;
  6. }
  7. #progressbar{
  8. position: absolute;
  9. top:50%;
  10. left:50%;
  11. margin-left:-90px;
  12. margin-top:-10px;
  13. width:180px;
  14. height:20px;
  15. border:1px solid darkgray;
  16. }
  17. /*在进度条元素上调用动画*/
  18. #fill{
  19. animation: move 2s;
  20. text-align: center;
  21. background-color: #6caf00;
  22. }
  23. /*实现元素宽度的过渡动画效果*/
  24. @keyframes move {
  25. 0%{
  26. width:0;
  27. }
  28. 100%{
  29. width:100%;
  30. }
  31. }

js代码:

  1. var progressbar={
  2. init:function(){
  3. var fill=document.getElementById('fill');
  4. var count=0;
  5. //通过间隔定时器实现百分比文字效果,通过计算CSS动画持续时间进行间隔设置
  6. var timer=setInterval(function(e){
  7. count++;
  8. fill.innerHTML=count+'%';
  9. if(count===100) clearInterval(timer);
  10. },17);
  11. }
  12. };
  13. progressbar.init();

效果图:

1.gif

总结:以上就是本篇文章所介绍的css+js实现简单的动态进度条效果的全部内容,大家可以自己动手试试,加深理解,制作不同的进度条效果,希望能对大家的学习有所帮助。

相关推荐:

html5如何实现简单进度条效果?动态进度条的实现

css中clip属性是什么?clip:rect()制作圆形进度条动画

js实现自定义拖动进度条效果

以上就是css+js如何实现简单的动态进度条效果?(代码实例)的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行