javaScript拖动验证

<!DOCTYPE html>
<html>
<head>
	<title></title>
</head>
<style type="text/css">
	#box{
		width:200px;
		height: 35px;
		margin:  0 auto;
		position: relative;
		box-sizing: border-box;
		border: 1px solid #ccc;
	}
	.bg{
		position: absolute;
		top: 0;
		left: 0;
		height: 100%;
		background-color: green;
	}
	.btn{
		position: absolute;
		top:0;
		left: 0;
		width: 100%;
		height: 100%;
		width: 50px;
		box-sizing: border-box;
		border: 1px solid #ccc;
		background-color: #333;
	}
	.text{
		line-height: 35px;
		text-align: center;
		color: red;
		position: absolute;
		width: 100%;
	}
</style>
<body>
	<div id="box">
		<div class="bg"></div>
		<div class="text">拖动验证</div>
		<div class="btn"></div>
	</div>

</body>
<script type="text/javascript">
	function init(){
		var btn = document.querySelector('.btn');
		var bg = document.querySelector('.bg');
		var text = document.querySelector('.text');
		var width = document.querySelector('#box').offsetWidth;
		var maxLeft = document.querySelector('#box').offsetWidth - btn.offsetWidth;
		var startX = 0;	
		var isDown = false;
		function mousedown(e){
			startX  = e.clientX;
			isDown = true;
		}

		function mousemove(e){
			if(!isDown) return false;
			var left  = e.clientX - startX;
			if(left > 0){
				btn.style.left = left + 'px';
				bg.style.width = left + 'px';
				if(left >= maxLeft){
					text.innerText = '验证通过';
					text.style.color = '#fff';
					remove();
				}
			}
		}

		function remove(){
			btn.removeEventListener('mousedown',mousedown,null);
			document.removeEventListener('mousemove',mousemove,null);
			document.removeEventListener('mouseup',mouseup,null);
		}

		function mouseup (e){
			var clientX = e.clientX - startX;
			if(clientX < maxLeft){
				isDown = false;
				btn.style.left = 0 + 'px';
				bg.style.width = 0 + 'px';
			}else{
				remove();
			}
		}

		btn.addEventListener('mousedown',mousedown,null);
		document.addEventListener('mousemove',mousemove,null);
		document.addEventListener('mouseup',mouseup,null)
	}
	document.addEventListener('readystatechange',init,null)
</script>
</html>

封装后

	function Drag(conf){
		this.btn = this.getDom(conf.btn);
		this.bg = this.getDom(conf.bg);
		this.text = this.getDom(conf.text);
		this.box  = this.getDom(conf.box );
		this.width = this.box.offsetWidth;
		this.maxLeft = this.getDom(conf.box).offsetWidth - this.btn.offsetWidth;
		this.startX = 0;	
		this.isDown = false;
		this.success = typeof  conf.success == 'function' ? conf.success : null;

		this.mousedown = this.mousedown.bind(this);
		this.mousemove = this.mousemove.bind(this);
		this.mouseup = this.mouseup.bind(this);
		this.remove = this.remove.bind(this);

		this.btn.addEventListener('mousedown',this.mousedown,null);
		document.addEventListener('mousemove',this.mousemove,null);
		document.addEventListener('mouseup',this.mouseup,null)
	}
	Drag.prototype = {
		getDom:document.querySelector.bind(document),
		getDomAll:document.querySelectorAll.bind(document),
		mousedown:function (e){
			this.startX  = e.clientX;
			this.isDown = true;
		},
		mousemove:function (e){
			if(!this.isDown) return false;
			var left  = e.clientX - this.startX;
			if(left > 0){
				this.btn.style.left = left + 'px';
				this.bg.style.width = left + 'px';
				if(left >= this.maxLeft){
					this.text.innerText = '验证通过';
					this.text.style.color = '#fff';
					this.remove();
				}
			}
		},
		remove:function (){
			this.isDown = false;
			this.btn.removeEventListener('mousedown',this.mousedown,null);
			document.removeEventListener('mousemove',this.mousemove,null);
			document.removeEventListener('mouseup',this.mouseup,null);
			if(this.success != null){
				this.success();
			}
		},
		mouseup:function (e){
			var clientX = e.clientX - this.startX;
			if(clientX < this.maxLeft){
				this.isDown = false;
				this.btn.style.left = 0 + 'px';
				this.bg.style.width = 0 + 'px';
			}else{
				//this.remove.call(this);
			}
		}
	}

	function init(){
		var drag = new Drag({
			box:'#box',
			btn:'.btn',
			bg:'.bg',
			text:'.text',
			success:function(){
				console.log('验证成功')
			}
		});
	}
	document.addEventListener('readystatechange',init,null)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值