欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

jQuery實(shí)現(xiàn)可拖動進(jìn)度條實(shí)例代碼

 更新時間:2017年06月21日 16:24:08   作者:Storm丶HYL  
這篇文章主要介紹了jQuery實(shí)現(xiàn)可拖動進(jìn)度條實(shí)例代碼,非常不錯,具有參考借鑒價值,需要的朋友可以參考下

html

   <div class="progress">
   <div class="progress_bg">
    <div class="progress_bar"></div>
   </div>
   <div class="progress_btn"></div>
   <div class="text">0%</div>
  </div>

css

  .progress{position: relative; width:300px;margin:100px auto;}
  .progress_bg{height: 10px; border: 1px solid #ddd; border-radius: 5px; overflow: hidden;background-color:#f2f2f2;}
  .progress_bar{background: #5FB878; width: 0; height: 10px; border-radius: 5px;}
  .progress_btn{width: 20px; height: 20px; border-radius: 5px; position: absolute;background:#fff; 
  left: 0px; margin-left: -10px; top:-5px; cursor: pointer;border:1px #ddd solid;box-sizing:border-box;}
  .progress_btn:hover{border-color:#F7B824;}

js 

$(function(){
    var tag = false,ox = 0,left = 0,bgleft = 0;
    $('.progress_btn').mousedown(function(e) {
     ox = e.pageX - left;
     tag = true;
    });
    $(document).mouseup(function() {
     tag = false;
    });
    $('.progress').mousemove(function(e) {//鼠標(biāo)移動
     if (tag) {
      left = e.pageX - ox;
      if (left <= 0) {
       left = 0;
      }else if (left > 300) {
       left = 300;
      }
      $('.progress_btn').css('left', left);
      $('.progress_bar').width(left);
      $('.text').html(parseInt((left/300)*100) + '%');
     }
    });
    $('.progress_bg').click(function(e) {//鼠標(biāo)點(diǎn)擊
     if (!tag) {
      bgleft = $('.progress_bg').offset().left;
      left = e.pageX - bgleft;
      if (left <= 0) {
       left = 0;
      }else if (left > 300) {
       left = 300;
      }
      $('.progress_btn').css('left', left);
      $('.progress_bar').animate({width:left},300);
      $('.text').html(parseInt((left/300)*100) + '%');
     }
    });
   });

效果圖

實(shí)現(xiàn)原理

  首先是用mousedown()鼠標(biāo)按下事件保存一個狀態(tài)值,mouseup()鼠標(biāo)抬起事件取消該狀態(tài),再同時配合mousemove()鼠標(biāo)移動事件,實(shí)現(xiàn)按住拖動的效果。

在鼠標(biāo)移動的同時去改變精度條的長度和按鈕的相對左部的距離。

  然后就是距離的計(jì)算,主要利用的就是pageX() 屬性。pageX是鼠標(biāo)指針相對于文檔的左邊緣的位置。在鼠標(biāo)按下是就記錄相對位置,在鼠標(biāo)移動后就可求出鼠標(biāo)移動的距離。從而改變按鈕位置和進(jìn)度條長度。

以上所述是小編給大家介紹的jQuery實(shí)現(xiàn)可拖動進(jìn)度條demo,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評論