用JS写一个简单滑动按钮的思路和方法是什么
Admin 2022-06-17 群英技术资讯 1244 次浏览
这篇文章主要介绍了用JS写一个简单滑动按钮的思路和方法是什么相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇用JS写一个简单滑动按钮的思路和方法是什么文章都会有所收获,下面我们一起来看看吧。利用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进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
目录自定义组件@click点击失效问题描述原因分析问题解决@click不起作用,同时报错自定义组件@click点击失效问题描述自定义组件在使无法,无法绑定原vue文件内的原生click事件原因分析根据查询vue官方文档,得知click事件作用于组件内部,如果组件内没有写click事件,便会无响应。问题解决1.参照官方文
首先我们来看一道题目,如下javascript代码,执行后会在控制台打印出什么内容?async function async1() { console.log(async1 start); await async2(); console.log(async1 end); } async function a
本文实例为大家分享了VUE递归树形实现多级列表,供大家参考,具体内容如下什么是递归?简单来说就是在组件中内使用组件本身。为什么要用递归?如果出现很多下拉菜单,同级,分级数据,层级混杂,可以使用v-for的嵌套循环不就完事了。对,没毛病这样的话也可以做,但是如果数据又多加了【很多】级分类呢;使用v-for也能实现,但是代
这篇文章主要为大家详细介绍了js实现文字无缝轮播,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
这篇文章给大家分享的是有关nodejs错误处理过程的内容。小编觉得挺实用的,下文有具体实例可以个参考,感兴趣的朋友不妨跟随小编一起看看吧。
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008