jQuery環形旋轉載入進度條效果

螞蟻小編發表於2018-05-24

本章節分享一段程式碼例項,它實現了進度條載入效果。

通過jquery來操控css3樣式設定,實現了環形旋轉,並且帶有百分比。

程式碼如下:

[HTML] 純文字檢視 複製程式碼執行程式碼
<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.softwhy.com/" />
<title>螞蟻部落</title>
<style>
* {
  margin: 0;
  padding: 0;
}
.circle {
  width: 200px;
  height: 200px;
  position: absolute;
  border-radius: 50%;
  background: #FFC000;
}
.pie_left, .pie_right {
  width: 200px;
  height: 200px;
  position: absolute;
  top: 0;
  left: 0;
}
.left, .right {
  display: block;
  width: 200px;
  height: 200px;
  background: #000;
  border-radius: 50%;
  position: absolute;
  top: 0;
  left: 0;
}
.pie_right, .right {
  clip: rect(0 auto auto 100px);
  clip: rect(0,auto,auto,100px);
}
.pie_left, .left {
  clip: rect(0 100px auto 0);
  clip: rect(0,100px,auto,0);
}
.mask {
  width: 150px;
  height: 150px;
  border-radius: 50%;
  left: 25px;
  top: 25px;
  background: #FFF;
  position: absolute;
  text-align: center;
  line-height: 150px;
  font-size: 16px;
  transition: all ease-in;
}
</style>
<script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script>
<script type="text/javascript">
$(function() {
  var indexCurrent = 0;
  setInterval(function(){
    if(indexCurrent<100){
      indexCurrent++;
    }else{
      indexCurrent=0;
    }
    $(".mask").find('span').text(indexCurrent);
    $('.circle').each(function(index, el) {
      var num = $(this).find('span').text() * 3.6;
      if (num<=180) {
        $(this).find('.right').css('transform', "rotate(" + num + "deg)");
        $(this).find('.left').css('transform', "rotate(" + 0 + "deg)");
      } else {
        $(this).find('.right').css('transform', "rotate(180deg)");
        $(this).find('.left').css('transform', "rotate(" + (num - 180) + "deg)");
      };
    });
  },50);
});
</script>
</head>
<body>
<div class="a"><div class="b"></div></div>
<div class="circle">
  <div class="pie_left"><div class="left"></div></div>
  <div class="pie_right"><div class="right"></div></div>
  <div class="mask"><span>1</span>%</div>
</div>
</body>
</html>

相關文章