怎样用JavaScript实现滑块验证功能?
Admin 2021-09-09 群英技术资讯 953 次浏览
今天给大家分享的是用JavaScript实现滑块验证功能,在实际的项目中,常常会遇到验证功能的需求,因此分享给大家做个参考。下文JavaScript实现登录的滑块验证代码,感兴趣的朋友可以参考,接下来我们一起来详细看看吧。
html代码
<div class="login-select"> <div v-show="errselectFlag" id="err-select"></div> <p id="title-p">按住滑块,拖拽验证</p> <div id="left-select"></div> <div id="right-select"> <i id="icon-dui" class="iconfont icon-right"></i> </div> </div>
javascript代码
// 滑块验证 var oRight = document.getElementById("right-select"); var bg = document.getElementById("left-select"); var title = document.getElementById("title-p"); var i = document.getElementById("icon-dui"); oRight.onmousedown = function (e) { var downX = e.clientX; //按下按钮后与窗口的x轴间距 // 鼠标移动事件 oRight.onmousemove = function (e) { if (e.clientX != 240) { oRight.style.left = 0 + "px"; bg.style.left = 0 + "px"; } var moveX = e.clientX - downX; //滑动的时候距离窗口的x轴的间距 //只有在大于0的时候才拖动,否则会出现反向拖动 if (moveX > 0) { oRight.style.left = moveX + "px"; //滑块与左边的距离 bg.style.width = moveX + "px"; //背景的宽度就是滑块距离左边的位置 if (moveX >= 280 - oRight.offsetWidth) { i.className = "iconfont icon-xingzhuang"; i.style.color = "rgb(86, 192, 15)"; title.innerText = "验证通过"; title.style.color = "#fff"; oRight.onmousemove = null; oRight.onmousedown = null; } } }; };
style代码
注:样式为sass文件
*{margin: 0;padding: 0;box-sizing: border-box;} .login-select { width: 280px; height: 40px; margin: auto; margin-top: 20px; margin-left: 15px; margin-right: 15px; text-align: center; line-height: 40px; background: rgba(134, 134, 131, 0.6); display: flex; position: relative; #err-select { width: 138px; height: 38px; position: absolute; right: -152px; top: 0; color: #fff; font-size: 12px; border-radius: 5px; line-height: 38px; text-align: center; background: rgb(177, 71, 71); } #title-p { text-align: center; line-height: 40px; width: 100%; height: 100%; font-size: 14px; position: absolute; } #left-select { width: 0; height: 100%; transform: translate(0.3s); background: rgb(86, 192, 15); } #right-select { width: 40px; height: 40px; background: #fff; color: #aaaa; text-align: center; line-height: 40px; border: 1px solid #ccc; position: absolute; cursor: move; } }
以上就是关于JavaScript实现登录滑块验证的方法,需要的朋友可以参考上述代码,希望本文对大家学习JavaScript有帮助,想要了解更多JavaScript实现滑块验证的内容,大家可以关注群英网络其它相关文章。
文本转载自脚本之家
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
本文主要介绍了JavaScript中let与const命令使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
vue清除定时器的方法是什么?一些朋友可能不知道为什么要清楚定时器,这是因为定时器如果不及时合理地清除,会造成业务逻辑混乱甚至应用卡死的情况,因此这篇文章就给大家分享关于vue清除定时器的方法及要注意的问题。
本文给大家介绍Vue 实现穿梭框功能,代码分为css,html和js代码,代码简单易懂,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧
这篇文章主要介绍了Node.js的概念和基本原理,对Node.js感兴趣的同学,一定要看一下
ES5和ES6中的apply异同在哪?在ES6中,新增了一个全局、内建、不可构造的Reflect对象,而且还提供了其下一系列可被拦截的操作方法。而其中的一个方法就是Reflect.apply(),我们知道ES5中也有apply,那么Reflect.apply()和ES5的Function.prototype.apply()有哪些区别和相同的地方呢?
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008