用JS写一个简单滑动按钮的思路和方法是什么
Admin 2022-06-17 群英技术资讯 813 次浏览
利用Js制作的滑动按钮的具体代码,供大家参考,具体内容如下
首先贴上效果图
再贴上源码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <div style="position: relative;width:100vw;height:100vh"> <div id="container"> <svg style="width:inherit;height:inherit"> <circle id="c" cx="25" cy="25" r="23" style="fill:white; stroke:gray; stroke-width:2;" onmousedown="down(event)" onmouseup="up(event)" onmouseleave="up(event)" /> </svg> </div> </div> <!-- <script> setTimeout(function () { let c = document.querySelector('circle'); console.log(c.parentNode.parentNode.style) },500) </script> --> <style> body{ margin:0; background-color:azure; } #container{ position:absolute; left: 50%; top: 50%; transform: translateX(-50%) translateY(-50%); width: 200px; height: 50px; background-color: black; border-radius: 50px; } </style> <script> let circle = document.getElementById('c'), clicked = false, x = 0,y = 0; circle.addEventListener("mousemove",function(e){ x = e.offsetX; if(clicked){ circle.setAttribute("cx",x) } }) function t(e){ circle.setAttribute("cx",e.offsetX); } function down(e){ clicked = true; } function up(){ if(clicked){ let flag; if(x <= 100) new Promise(function(resolve,reject){ flag = setInterval(function(){ x -= 2; circle.setAttribute("cx",x); if(x <= 25){ circle.setAttribute("cx",25) circle.setAttribute("style","fill:white; stroke:gray; stroke-width:2;") resolve("ok") } }) }).then(res => { clearInterval(flag) }) else new Promise(function(resolve,reject){ flag = setInterval(function(){ x += 2; circle.setAttribute("cx",x); if(x >= 175){ circle.setAttribute("cx",175); circle.setAttribute("style","fill:black; stroke:gray; stroke-width:2;") resolve("ok") } }) }).then(res => { clearInterval(flag) }) } clicked = false; } </script> </body> </html>
知识点和制作思路及步骤
1、基本布局(父相子绝,left: 50%; top: 50%; transform: translateX(-50%)
translateY(-50%);)
2、svg的circle( cx )控制移动, 对于circle的cx采用setAtrribute来进行控制。
3、**Promise.then()**用来保证结束后进行clearInterval
4、circle监听了mousemove,mouseup,mousedown事件。 当mousedown事件触发会将cliked置为true进而move事件会进行reset;
5、mouseup和mouseleave会将cliked置为false;进而无法触发move事件的reset(停止);
6、当停止状态下,判断原点在左侧还是右侧, 动画: 如果在左半部分则利用setInterval进行10ms一帧每次1.5px的移动,判断到达开始或者结束点则停止。
7、再进行样式切换。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
用jQuery怎样验证输入是不是数字?在登录注册页面,我们通常需要对输入验证,那么我们怎样知道输入的是不是数字呢?下文给大家分享一个简单的验证数字示例,有一定参考价值,需要的朋友可以参考看看。
本文实例为大家分享了JS旋转实现转盘抽奖效果的具体代码,供大家参考,具体内容如下闲来没事,做了一个模拟转盘抽奖的HTML&JS的效果:可以在设置的时候,选择几个区域,并且可以填写指针将要停止的区域
获取标签中value的值使用的javascript获取,所以肯定是包含在 script 标签里的,也需要引入jquery标签示例:input id=goodsCount type=text size=2 readonly=readonly class=num-text value=#{num}获取其中value的值:/
jQuery中的$符号表示什么?日常中我们看到$符号是表示美元的符号,但是在jQuery中$符号是代表获取的意思,是一个回传函数。这篇我们就来了解看看$符号怎么样使用。
v-for 指令需要使用 item in items 形式的特殊语法,其中 items 是源数据数组,而 item 则是被迭代的数组元素的别名。今天通过本文给大家介绍Vue中v-for更新检测的操作方法,感兴趣的朋友一起看看吧
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008