表单滑动验证

7 篇文章 0 订阅
4 篇文章 0 订阅

滑动验证

效果—↓↓↓
滑动前
开始
滑动中
滑动
滑动完成
结束
代码

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<title>滑动验证</title>
		<link rel="stylesheet" type="text/css" href="./css/public.css" />
		<style type="text/css">
			.content {
				width: 100%;
			}

			.box {
				margin: 50px auto 0 auto;
				width: 220px;
			}

			.range {
				width: 222px;
				height: 32px;
				border: 1px solid #e4e7ed;
				overflow: hidden;
			}

			.dom {
				width: 220px;
				height: 30px;
				display: flex;
				align-items: center;
				margin-left: 0;
				transform: translateX(-180px);
			}

			.progress-bar {
				background: linear-gradient(45deg, #f43f3b, #ec008c);
				width: 180px;
				height: 30px;
			}

			.button {
				line-height: 30px;
				width: 40px;
				height: 30px;
				text-align: center;
				background: linear-gradient(45deg, #39b54a, #8dc63f);
				color: #ffffff;
				cursor: pointer;
				/* 禁止文本被选中 */
				-moz-user-select: none;
				-webkit-user-select: none;
				-ms-user-select: none;
				-khtml-user-select: none;
				user-select: none;
				-webkit-touch-callout: none;
				-webkit-user-select: none;
				-khtml-user-select: none;
				-moz-user-select: none;
				-ms-user-select: none;
				user-select: none;
			}
		</style>
	</head>
	<body>
		<div class="content">
			<div class="box">
				<div class="range">
					<div class="dom" id="domId">
						<div class="progress-bar" id="progress-bar"></div>
						<div class="button" id="buttonId">-></div>
					</div>
				</div>
			</div>
		</div>
	</body>
	<script type="text/javascript">
		let button = document.getElementById('buttonId')
		let dom = document.getElementById('domId')
		let progressBar = document.getElementById('progress-bar')
		let clientX = 0, startX = 0, endX = 0;
		let width = progressBar.offsetWidth
		window.onload = function(){
			button.onmousedown =function(ev){//鼠标按下
				startX = ev.clientX
				document.onmousemove = function(ev){//为了防止鼠标移动太快而离开了DIV产生了bug,所以要给整个页面加onmousemove事件
					endX = ev.clientX - startX
					if (endX < 0) {
						endX = 0
					} else if(endX > width) {
						endX = width
					}
					clientX = endX
					dom.style.marginLeft = clientX + 'px'
				};
				document.onmouseup = function(){//鼠标松开时
					document.onmousemove = null;//把鼠标移动清除
					document.onmouseup = null;//把鼠标松开清除
					if (clientX < width) {
						dom.style.marginLeft = 0
					} else if (clientX == width) {
						alert('验证成功!')
						button.onmousedown = null
					}
				};
				return false;
			};
		}
	</script>
</html>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DevilAngelia

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值