您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

jQuery实现可拖动进度条详解

2024/3/15 15:30:52发布22次查看
本文主要介绍了jquery实现可拖动进度条实例代码,非常不错,具有参考借鉴价值,需要的朋友可以参考下,希望能帮助到大家。
html
<p class="progress">    <p class="progress_bg">     <p class="progress_bar"></p>    </p>    <p class="progress_btn"></p>    <p class="text">0%</p>   </p>
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) {//鼠标移动      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) {//鼠标点击      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) + '%');      }     });    });
效果图
实现原理
首先是用mousedown()鼠标按下事件保存一个状态值,mouseup()鼠标抬起事件取消该状态,再同时配合mousemove()鼠标移动事件,实现按住拖动的效果。
在鼠标移动的同时去改变精度条的长度和按钮的相对左部的距离。
然后就是距离的计算,主要利用的就是pagex() 属性。pagex是鼠标指针相对于文档的左边缘的位置。在鼠标按下是就记录相对位置,在鼠标移动后就可求出鼠标移动的距离。从而改变按钮位置和进度条长度。
相关推荐:
实例讲解canvas实现圆形进度条动画
实现jquery-file-upload 文件上传带进度条效果的方法
微信小程序实现下载进度条的方法
以上就是jquery实现可拖动进度条详解的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product