您好,登录后才能下订单哦!
小编给大家分享一下原生js怎么实现的移动端可拖动进度条插件功能,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
具体如下:
该插件最初的想法来自网上的一篇文章,直达链接://www.jb51.net/article/167717.htm
笔者因为业务需要寻找到这个插件,然后拿来用之,发现各种不方便,然后便开始了改造之路。
上代码:
<script> function dragSlide(id) { this.minDiv =document.getElementById(id); //小方块 this.width = parseInt(window.getComputedStyle(this.minDiv, null).width); //小方块的宽度 this.lineDiv = this.minDiv.parentNode; //长线条 //滑动的数值呈现 this.vals = this.minDiv.children[0]; var that=this; var move = function(e) { var x = e.touches[0].pageX; var lineDiv_left = that.getPosition(that.lineDiv).left; //长线条的横坐标 var minDiv_left = x - lineDiv_left; //小方块相对于父元素(长线条)的left值 if (minDiv_left >= that.lineDiv.offsetWidth - that.width) { minDiv_left = that.lineDiv.offsetWidth - that.width; } if (minDiv_left <0 ) { minDiv_left = 0; } //设置拖动后小方块的left值 that.minDiv.style.left = minDiv_left + "px"; //percent百分比改为如下所示,解决开始和最后滑动的体验不好问题 var percent = (minDiv_left / (that.lineDiv.offsetWidth - that.width)) * 100; if (percent > 0 && percent < 0.5) { percent = Math.ceil(percent); } else { percent = Math.floor(percent); } that.vals.innerText = percent; } //获取元素的绝对位置,工具函数 this.getPosition = function(node) { var left = node.offsetLeft; //获取元素相对于其父元素的left值var left var top = node.offsetTop; current = node.offsetParent; // 取得元素的offsetParent // 一直循环直到根元素 while (current != null) { left += current.offsetLeft; top += current.offsetTop; current = current.offsetParent; } return { "left": left, "top": top }; } this.minDiv.addEventListener("touchmove", move); } var drag0 = new dragSlide("minDiv"); var drag1 = new dragSlide("minDiv1"); //取消移动端手势长按弹出提示框的操作 document.addEventListener('contextmenu', function(e) { e.preventDefault(); }); </script>
html和css部分没有改动,而js改动还是很大的,比较原来作者的文章,改动点如下
1)整体上,原来不是插件,改造之后成为一个可以复用的插件,虽然简单了点
2)只是将其改造为适用于移动端的插件
3)通过对开始滑动和结束滑动比例的处理,优化了开始滑动和结束滑动的体验
4)移动端加了防止长按弹出提示框的代码
5)小滑块的宽度改为动态
改造之后的整体案例,需要指出:笔者主要用在微信端,至于其他浏览器滑块的体验不是很好,还有滑块滑动体验跟小块的尺寸有直接关系。
<!DOCTYPE html> <html lang="zh-cn"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" /> <title>鼠标拖动小方块</title> <style type="text/css"> .lineDiv { position: relative; height: 5px; background: red; width: 300px; margin: 50px auto; } .lineDiv .minDiv { position: absolute; top: -12.5px; left: 0; width: 30px; height: 30px; background: green; cursor: pointer } .lineDiv .minDiv .vals { position: absolute; font-size: 20px; top: -45px; left: -2.5px; width: 35px; height: 35px; line-height: 35px; text-align: center; background: blue; } .lineDiv .minDiv .vals:after { content: ""; width: 0px; height: 0px; border-top: 6px solid blue; border-left: 6px solid transparent; border-right: 6px solid transparent; border-bottom: 6px solid transparent; display: block; margin-left: 11px; } * { -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; } </style> </head> <body> <center> <h4>用鼠标拖动小方块<span id="msg">0</span>%</h4> </center> <div id="lineDiv" class="lineDiv"> <div id="minDiv" class="minDiv"> <div id="vals" class="vals">0</div> </div> </div> <div ></div> <div id="lineDiv" class="lineDiv"> <div id="minDiv1" class="minDiv"> <div id="vals" class="vals">0</div> </div> </div> <script> //避免默认事件 2018.7.10 更新 优化uc浏览器左右滑动时候页面被拖动 document.addEventListener('touchmove', function(e) { e.preventDefault(); }, { passive: false }); function dragSlide(id) { this.minDiv = document.getElementById(id); //小方块 this.width = parseInt(window.getComputedStyle(this.minDiv, null).width); //小方块的宽度 this.lineDiv = this.minDiv.parentNode; //长线条 //滑动的数值呈现 this.vals = this.minDiv.children[0]; var that = this; var lastX = null; //判断鼠标移动方向,解决向左侧滑动时候的bug var move = function(e) { var x = e.touches[0].pageX, direction = ''; if (lastX == null) { lastX = x; return; } if (x > lastX) { direction = 'right'; } else if (x < lastX) { direction = 'left'; } else { direction = ''; } var lineDiv_left = that.getPosition(that.lineDiv).left; //长线条的横坐标 var minDiv_left = x - lineDiv_left; //小方块相对于父元素(长线条)的left值 if (minDiv_left >= that.lineDiv.offsetWidth - that.width) { minDiv_left = that.lineDiv.offsetWidth - that.width; } if (minDiv_left < 0) { minDiv_left = 0; } //设置拖动后小方块的left值 that.minDiv.style.left = minDiv_left + "px"; //percent百分比改为如下所示,解决开始和最后滑动的体验不好问题 var percent = (minDiv_left / (that.lineDiv.offsetWidth - that.width)) * 100; if (percent < 0.5 && direction == 'right') { percent = Math.ceil(percent); } else if (percent > 0.5 && direction == 'right') { percent = Math.floor(percent); } else { percent = Math.ceil(percent); } that.vals.innerText = percent; } //获取元素的绝对位置,工具函数 this.getPosition = function(node) { var left = node.offsetLeft; //获取元素相对于其父元素的left值var left var top = node.offsetTop; current = node.offsetParent; // 取得元素的offsetParent // 一直循环直到根元素 while (current != null) { left += current.offsetLeft; top += current.offsetTop; current = current.offsetParent; } return { "left": left, "top": top }; } this.minDiv.addEventListener("touchmove", move); } var drag0 = new dragSlide("minDiv"); var drag1 = new dragSlide("minDiv1"); //取消移动端手势长按弹出提示框的操作 document.addEventListener('contextmenu', function(e) { e.preventDefault(); }); </script> </body> </html>
以上是“原生js怎么实现的移动端可拖动进度条插件功能”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。