jQuery進度條效果程式碼例項

antzone發表於2017-04-19

分享一段程式碼例項,它利用jQuery實現了進度條效果。

更多進度調效果可以查詢特效下載版塊進度條分類。

程式碼例項如下:

[HTML] 純文字檢視 複製程式碼執行程式碼
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="author" content="http://www.softwhy.com/" />
<title>螞蟻部落</title>
<style type="text/css">
#box{
  position: relative; 
  width: 500px; 
  height: 110px; 
  margin:50px auto 0;
}
 #bg{
  height: 8px; 
  background-color: #f4f4f4;
  border-radius:8px; 
  overflow: hidden;
}
 #bgcolor{
  background: #7ac6fa; 
  width:0; 
  height: 8px; 
  border-radius: 5px 0 0 5px;
}
#bt{
  width: 10px; 
  height: 10px; 
  background: #7ac6fa center center; 
  border-radius: 50%; 
  border:3px solid #fff;
  overflow: hidden; 
  position: absolute; 
  top:-4px;
  left:-10px;
  cursor: pointer;
}
#text{
  width: 200px; 
  margin: 0 auto; 
  font-size: 16px; 
  line-height: 2em;
}
</style>
<script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script>
</head> 
<body> 
 <div id="box">
  <div id="bg">
   <div id="bgcolor"></div>
  </div>
  <div id="bt"></div>
 </div>
 <div id="text"></div>
 <script type="text/javascript">
 (function($){
  var $box = $('#box');
  var $bg = $('#bg');
  var $bgcolor = $('#bgcolor');
  var $btn = $('#bt');
  var $text = $('#text');
  var statu = false;
  var ox = 0;
  var lx = 0;
  var left = 0;
  var bgleft = 0;
   $btn.mousedown(function(e){
    lx = $btn.offset().left;
    ox = e.pageX - left;
    statu = true;
   });
   $(document).mouseup(function(){
    statu = false;
   });
   $box.mousemove(function(e){
    if(statu){
     left = e.pageX - ox;
     if(left < 0){
      left = 0;
     }
     if(left > 500){
      left = 500;
     }
     $btn.css('left',left-10);
     $bgcolor.width(left);
     $text.html('位置:' + parseInt(left/5) + '%');
    }
   });
   $bg.click(function(e){
    if(!statu){
     bgleft = $bg.offset().left;
     left = e.pageX - bgleft;
     if(left < 0){
      left = 0;
     }
     if(left > 500){
      left = 500;
     }
     $btn.css('left',left-10);
     $bgcolor.stop().animate({width:left},500);
     $text.html('位置:' + parseInt(left/5) + '%');
    }
   });
 })(jQuery);
 </script>
</body> 
</html>

相關文章