1.前端页面应用滑块验证可以防止页面频繁向后台请求数据;
2.主要用到js事件:
onmousedown() 鼠标按下时响应
onmousemove() 鼠标移动时响应
onmouseup() 鼠标弹起时响应
3.获取页面距离:
e.clientx
obj.offsetwidth
obj.offsetleft
4.代码:
html:
<div class="box"> <div class="txt">滑块验证</div> <div class="btn">>></div> <div class="bg"></div> </div>
css:
*{ -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; } .box{ position: relative; width:300px; height:30px; background-color: #ccc; margin:20px auto; font-size:14px; line-height:30px; box-sizing:border-box; z-index:1; } .txt{ position: absolute; left: 50%; top:0; transform: translatex(-50%); color:blue; z-index:3; } .btn{ position: absolute; top:0; left: 0; width:40px; height:30px; border:1px solid #ccc; background-color: #fff; text-align: center; line-height: 30px; cursor: move; box-sizing: border-box; z-index:4; } .bg{ position: absolute; left: 0; top:0; width:0; height:30px; background-color:green; box-sizing: border-box; z-index:2; }
js:
window.onload = function(){ var box = document.queryselector(".box"), txt = document.queryselector(".txt"), btn = document.queryselector(".btn"), bg = document.queryselector(".bg"), end = false; btn.onmousedown = function(e){ var e = e || window.event; var point = e.clientx - box.offsetleft; btn.onmousemove = function(e){ var movew = e.clientx - box.offsetleft - point; btn.style.left = movew + "px"; bg.style.width = movew + "px"; if(btn.offsetleft<=0){ btn.style.left = "0"; } if(btn.offsetleft>=(box.clientwidth - btn.clientwidth)){ btn.style.left = box.clientwidth - btn.clientwidth txt.innerhtml = "验证完成"; btn.onmousemove = null; btn.onmousedown = null; end = true; } } btn.onmouseup = function(){ btn.onmousemove = null; if(!end){ btn.style.left = "0"; bg.style.width = "0"; } } } }
5.显示
如对本文有疑问, 点击进行留言回复!!
Android 入门第七讲01-数据存储(数据存储概述,文件存储(raw和asserts目录读写,data/data/包名目录读写,sdcard目录读写),SharedPreferences读写)
vue-axios系列:axios拦截器,配置请求头,配置请求参数
Vuejs 针对 安卓低版本 、ios9.x 不兼容 ES6语法导致失效解决方法
网友评论