基于JS如何制作页面滑动验证,代码是什么
Admin 2022-07-18 群英技术资讯 316 次浏览
本文实例为大家分享了炫酷的前端页面滑动验证的具体代码,供大家参考,具体内容如下
分享炫酷的前端页面滑动验证
前面已经发过一种,这里再发一种,只是特效不一样
还是直接上代码:
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> <style> /* 滑动控件容器,灰色背景 */ #dragContainer { position: relative; display: inline-block; background: #e8e8e8; width: 300px; height: 33px; border: 2px solid #e8e8e8; } /* 滑块左边部分,绿色背景 */ #dragBg { position: absolute; background-color: #7ac23c; width: 0px; height: 100%; } /* 滑动验证容器文本 */ #dragText { position: absolute; width: 100%; height: 100%; /* 文字水平居中 */ text-align: center; /* 文字垂直居中,这里不能用百分比,因为百分比是相对原始line-height的,而非div高度 */ line-height: 33px; /* 文本不允许选中 */ user-select: none; -webkit-user-select: none; } /* 滑块 */ #dragHandler { position: absolute; width: 40px; height: 100%; cursor: move; } /* 滑块初始背景 */ .dragHandlerBg { background: #fff no-repeat center url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAA3hpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuNS1jMDIxIDc5LjE1NTc3MiwgMjAxNC8wMS8xMy0xOTo0NDowMCAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wTU09Imh0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dG9zaG9wIENDIDIwMTQgKE1hY2ludG9zaCkiPiA8eG1wTU06RGVyaXZlZEZyb20gc3RSZWY6aW5zdGFuY2VJRD0ieG1wLmlpZDphNWEzMWNhMC1hYmViLTQxNWEtYTEwZS04Y2U5NzRlN2Q4YTEiIHN0UmVmOmRvY3VtZW50SUQ9InhtcC5kaWQ6NGQ4ZTVmOTMtOTZiNC00ZTVkLThhY2ItN2U2ODhmMjE1NmU2Ii8+IDwvcmRmOkRlc2NyaXB0aW9uPiA8L3JkZjpSREY+IDwveDp4bXBtZXRhPiA8P3hwYWNrZXQgZW5kPSJyIj8+k+sHwwAAASZJREFUeNpi/P//PwMyKD8uZw+kUoDYEYgloMIvgHg/EM/ptHx0EFk9I8wAoEZ+IDUPiIMY8IN1QJwENOgj3ACo5gNAbMBAHLgAxA4gQ5igAnNJ0MwAVTsX7IKyY7L2UNuJAf+AmAmJ78AEDTBiwGYg5gbifCSxFCZoaBMCy4A4GOjnH0D6DpK4IxNSVIHAfSDOAeLraJrjgJp/AwPbHMhejiQnwYRmUzNQ4VQgDQqXK0ia/0I17wJiPmQNTNBEAgMlQIWiQA2vgWw7QppBekGxsAjIiEUSBNnsBDWEAY9mEFgMMgBk00E0iZtA7AHEctDQ58MRuA6wlLgGFMoMpIG1QFeGwAIxGZo8GUhIysmwQGSAZgwHaEZhICIzOaBkJkqyM0CAAQDGx279Jf50AAAAAABJRU5ErkJggg=="); } </style> </head> <body> <div id="dragContainer"><!-- 容器初始背景 --> <div id="dragBg"></div><!-- 绿色背景 --> <div id="dragText"></div><!-- 滑动容器文本 --> <div id="dragHandler" class="dragHandlerBg"></div> </div> <!-- 滑块 滑块初始背景 --> </body> <script> //加载(事件会在页面加载完成后触发) window.onload = function() { //获取滑动控件容器,灰色背景 var dragContainer = document.getElementById("dragContainer"); //获取滑块左边部分,绿色背景 var dragBg = document.getElementById("dragBg"); //获取滑动验证容器文本 var dragText = document.getElementById("dragText"); //获取滑块 var dragHandler = document.getElementById("dragHandler"); //滑块的最大偏移量 = 滑动验证容器文本长度 - 滑块长度 var maxHandlerOffset = dragContainer.clientWidth - dragHandler.clientWidth; //是否验证成功的标记 var isVertifySucc = false; initDrag(); function initDrag() { //在滑动验证容器文本写入“拖动滑块验证” dragText.textContent = "拖动滑块验证"; //给滑块添加鼠标按下监听 dragHandler.addEventListener("mousedown", onDragHandlerMouseDown); } //选中滑块 function onDragHandlerMouseDown() { //鼠标移动监听 document.addEventListener("mousemove", onDragHandlerMouseMove); //鼠标松开监听 document.addEventListener("mouseup", onDragHandlerMouseUp); } //滑块移动 function onDragHandlerMouseMove() { /* html元素不存在width属性,只有clientWidth offsetX是相对当前元素的,clientX和pageX是相对其父元素的 */ //滑块移动量 var left = event.clientX - dragHandler.clientWidth / 2; // if(left < 0) { left = 0; //如果滑块移动量 > 滑块的最大偏移量 ,则调用验证成功函数 } else if(left > maxHandlerOffset) { left = maxHandlerOffset; verifySucc(); } //滑块移动量 dragHandler.style.left = left + "px"; //绿色背景的长度 dragBg.style.width = dragHandler.style.left; } //松开滑块函数 function onDragHandlerMouseUp() { //移除鼠标移动监听 document.removeEventListener("mousemove", onDragHandlerMouseMove); //移除鼠标松开监听 document.removeEventListener("mouseup", onDragHandlerMouseUp); //初始化滑块移动量 dragHandler.style.left = 0; //初始化绿色背景 dragBg.style.width = 0; } //验证成功 function verifySucc() { //成功标记,不可回弹 isVertifySucc = false; //容器文本的文字改为白色“验证通过”字体 dragText.textContent = "验证通过"; dragText.style.color = "white"; //验证通过的滑块背景 dragHandler.setAttribute("class", "dragHandlerOkBg"); //移除鼠标按下监听 dragHandler.removeEventListener("mousedown", onDragHandlerMouseDown); //移除 鼠标移动监听 document.removeEventListener("mousemove", onDragHandlerMouseMove); //移除鼠标松开监听 document.removeEventListener("mouseup", onDragHandlerMouseUp); // 匹配成功以后写入你要跳转的页面 //window.location.href="成功页面.html" rel="external nofollow" ; }; } </script> </html>
效果如下
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
最近做了一个手机端系统,其中遇到了父页面需要携带参数跳转至子页面的问题,现已解决,下面分享一下实现过程,感兴趣的朋友一起看看吧
这篇文章主要为大家介绍了用java编程来写一款JavaScript表格插件,非常的使用,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
这篇文章主要给大家介绍了关于Nest.js散列与加密的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
JavaScript中关系运算符有几种?JavaScript关系运算符有==、===、!=、!==、<、>等等,这些符号都表示什么意思呢?用法和使用注意事项是什么?这篇文章小编就给大家来简单的介绍一下,有需要的朋友也可以参考,接下来就跟随小编一起了解看看吧。。
二维数组本质上是以数组作为数组元素的数组,即"数组的数组",类型说明符 数组名[常量表达式][常量表达式]。二维数组又称为矩阵,行列数相等的矩阵称为方阵。对称矩阵a[i][j] = a[j][i],对角矩阵:n阶方阵主对角线外都是零元素
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008