原生js实现拖动滑块验证

拖动滑块验证是现在的网站随处可见的,各式各样的拖动法都有。

下面实现的是某宝的拖动滑块验证:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
.drag{position: relative;width: 300px;height: 34px;background-color: #e8e8e8;line-height: 34px;}
.bg{position: absolute;width: 40px;height: 100%;background-color: #7ac23c;}
.text{position: absolute;width: 100%;margin: 0;text-align: center;}
.btn{position: absolute;width: 40px;height: 32px;background: #fff url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAA3hpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuNS1jMDIxIDc5LjE1NTc3MiwgMjAxNC8wMS8xMy0xOTo0NDowMCAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wTU09Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9tbS8iIHhtbG5zOnN0UmVmPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvc1R5cGUvUmVzb3VyY2VSZWYjIiB4bWxuczp4bXA9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC8iIHhtcE1NOk9yaWdpbmFsRG9jdW1lbnRJRD0ieG1wLmRpZDo0ZDhlNWY5My05NmI0LTRlNWQtOGFjYi03ZTY4OGYyMTU2ZTYiIHhtcE1NOkRvY3VtZW50SUQ9InhtcC5kaWQ6NTEyNTVEMURGMkVFMTFFNEI5NDBCMjQ2M0ExMDQ1OUYiIHhtcE1NOkluc3RhbmNlSUQ9InhtcC5paWQ6NTEyNTVEMUNGMkVFMTFFNEI5NDBCMjQ2M0ExMDQ1OUYiIHhtcDpDcmVhdG9yVG9vbD0iQWRvYmUgUGhvdG9zaG9wIENDIDIwMTQgKE1hY2ludG9zaCkiPiA8eG1wTU06RGVyaXZlZEZyb20gc3RSZWY6aW5zdGFuY2VJRD0ieG1wLmlpZDo2MTc5NzNmZS02OTQxLTQyOTYtYTIwNi02NDI2YTNkOWU5YmUiIHN0UmVmOmRvY3VtZW50SUQ9InhtcC5kaWQ6NGQ4ZTVmOTMtOTZiNC00ZTVkLThhY2ItN2U2ODhmMjE1NmU2Ii8+IDwvcmRmOkRlc2NyaXB0aW9uPiA8L3JkZjpSREY+IDwveDp4bXBtZXRhPiA8P3hwYWNrZXQgZW5kPSJyIj8+YiRG4AAAALFJREFUeNpi/P//PwMlgImBQkA9A+bOnfsIiBOxKcInh+yCaCDuByoswaIOpxwjciACFegBqZ1AvBSIS5OTk/8TkmNEjwWgQiUgtQuIjwAxUF3yX3xyGIEIFLwHpKyAWB+I1xGSwxULIGf9A7mQkBwTlhBXAFLHgPgqEAcTkmNCU6AL9d8WII4HOvk3ITkWJAXWUMlOoGQHmsE45ViQ2KuBuASoYC4Wf+OUYxz6mQkgwAAN9mIrUReCXgAAAABJRU5ErkJggg==) center no-repeat;border: 1px solid #ccc;cursor: pointer;}
</style>
</head>
<body>
<div class="drag">
<div class="bg"></div>
<p class="text">拖动滑块验证</p>
<div class="btn"></div>
</div> <script>
var $ = function(selector){
return document.querySelector(selector);
},
box = $('.drag'),//容器
bg = $('.bg'),//绿色背景
text = $('.text'),//文字
btn = $('.btn'),//拖动按钮
done = false;//是否通过验证 btn.onmousedown = function(e){
var e = e || window.event;
var posX = e.clientX - this.offsetLeft; btn.onmousemove = function(e){
var e = e || event; var x = e.clientX - posX; this.style.left = x + 'px';
bg.style.width = this.offsetLeft + 'px'; // 通过验证
if(x >= box.offsetWidth - btn.offsetWidth){
done = true;
btn.onmousedown = null;
btn.onmousemove = null;
text.innerHTML = '通过验证';
}
}; document.onmouseup = function(){
btn.onmousemove = null;
btn.onmouseup = null; if(done)return;
btn.style.left = 0;
bg.style.width = 0;
}
}; text.onmousedown = function(){
return false;
}
</script>
</body>
</html>

由于是原生js实现的,因此没有加上动画效果,不然代码量太大。

代码简单,直接看下结构就明了了。

上一篇:Docker(12)- docker run 命令详解


下一篇:使用JDBC对数据库进行查询的前期准备工作,以及简单的JDBC访问MySQL数据库(Mac)