HTML+CSS+JS实现拖动进度条以及点击进度增加的功能

HTML: 

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

JS:

$(document).ready(function(){
 var tag = false,dx = 0,left = 0,bgleft = 0;
 $('.progress_btn').mousedown(function(e) {
  dx = e.pageX - left;
  tag = true;
 });
 $(document).mouseup(function(e) {
  tag = false;
 });
 $('.progress_btn').mousemove(function(e) {//鼠标移动
  if (tag) {
   left = e.pageX - dx;
   if (left <= 0) {
    left = 0;
   }else if (left > 300) {
    left = 300;
   }
   $('.progress_btn').css('left', left);
   $('.progress_bar').animate({width:left},1);
   $('.text').html(parseInt((left/300)*100) + '%');
  }
 });
 $('.progress_bg').click(function(e) {//鼠标点击
  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) + '%');
  }
//  alert(left);
 });

});

CSS:

.progress{position: absolute; width:300px;margin:100px auto;}
.progress_bg{height: 10px;width:300px; border: 1px solid #ddd; border-radius: 5px;overflow: hidden;background-color:#f2f2f2;}/**/
.progress_bar{ width: 0; height: 10px; border-radius: 5px;background: #5FB878;position: absolute; top:1px;}/* position: absolute;*/
.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;}

 

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值