效果:
原理:
给控件设置一个原始背景色(即解锁前背景颜色),在上面覆盖一层绿色背景色区域(相对于原始背景定位),通过监听鼠标偏移量left来不断的设置绿色区域的宽度,当没有滚动到最大偏移量松开时回到初始位置,达到最大偏移量时表示校验通过(此时改变校验文案及图形)
偏移量left的计算:
最大偏移量maxHandlerOffset:滑动元素宽度-滑块宽度
x: 绿色区域初始位置=this.dragBg?.getBoundingClientRect()?.left
x1: 鼠标移动滑块位置,默认在滑块中间=event.clientX
x2: 滑块位置=x1-滑块宽度一半(event.clientX - this.dragHandler.clientWidth / 2)
left: 最终计算的偏移量=x2-x
left<0时:left初始为0
left>=最大偏移量时:校验成功
完整代码:
<template>
<div class="flex-center">
<div id="dragContainer">
<!-- 绿色区域 -->
<div id="dragBg"></div>
<!-- 滑动容器文本 -->
<div id="dragText"></div>
<!-- 拖动元素 -->
<div id="dragHandler" class="dragHandlerBg"></div>
</div>
<div class="btn" @click="initDrag">初始化</div>
</div>
</template>
<script>
export default {
data() {
return {
dragContainer: null,
dragBg: null,
dragText: null,
dragHandler: null,
maxHandlerOffset: null,
}
},
mounted() {
//获取滑动控件容器,灰色背景
this.dragContainer = document.getElementById("dragContainer");
//获取滑块左边部分,绿色背景
this.dragBg = document.getElementById("dragBg");
//获取滑动验证容器文本
this.dragText = document.getElementById("dragText");
//获取滑块
this.dragHandler = document.getElementById("dragHandler");
//滑块的最大偏移量 = 滑动验证容器文本长度 - 滑块长度
this.maxHandlerOffset = dragContainer.clientWidth - dragHandler.clientWidth;
// 初始化
this.initDrag();
},
methods: {
// 初始化
initDrag() {
//在滑动验证容器文本写入“拖动滑块验证”
this.dragText.textContent = "拖动滑块验证";
//给滑块添加鼠标按下监听
this.dragHandler.addEventListener("mousedown", this.onDragHandlerMouseDown);
//初始化滑块移动量
this.dragHandler.style.left = 0;
//初始化绿色背景
this.dragBg.style.width = 0;
this.dragText.style.color = "";
this.dragHandler.setAttribute("class", "dragHandlerBg");
},
// 鼠标按下
onDragHandlerMouseDown() {
//鼠标移动监听
document.addEventListener("mousemove", this.onDragHandlerMouseMove);
//鼠标松开监听
document.addEventListener("mouseup", this.onDragHandlerMouseUp);
},
// 鼠标移动
onDragHandlerMouseMove() {
//滑块移动量
let dragX = this.dragBg?.getBoundingClientRect()||{}
var left = event.clientX - this.dragHandler.clientWidth / 2 - dragX.left;
if(left < 0) { // 滑动小于0设为0
left = 0;
} else if(left >= this.maxHandlerOffset) { // 滑动超过最大偏移量则校验成功
left = this.maxHandlerOffset;
this.verifySucc();
}
// 超过最大偏移量,不增加绿色区域宽度
if(left>this.maxHandlerOffset) return;
// 滑块移动量(距离左侧的定位)
this.dragHandler.style.left = left + "px";
// 绿色背景的长度
this.dragBg.style.width = left + "px";
},
// 鼠标抬起
onDragHandlerMouseUp() {
//移除鼠标移动监听
document.removeEventListener("mousemove", this.onDragHandlerMouseMove);
//移除鼠标松开监听
document.removeEventListener("mouseup", this.onDragHandlerMouseUp);
//初始化滑块移动量
this.dragHandler.style.left = 0;
//初始化绿色背景
this.dragBg.style.width = 0;
},
// 滑动完成
verifySucc() {
//容器文本的文字改为白色“验证通过”字体
this.dragText.textContent = "验证通过";
this.dragText.style.color = "white";
//验证通过的滑块背景
this.dragHandler.setAttribute("class", "dragHandlerOkBg");
//移除鼠标按下监听
this.dragHandler.removeEventListener("mousedown", this.onDragHandlerMouseDown);
//移除 鼠标移动监听
document.removeEventListener("mousemove", this.onDragHandlerMouseMove);
//移除鼠标松开监听
document.removeEventListener("mouseup", this.onDragHandlerMouseUp);
// alert('操作成功')
},
}
}
</script>
<style>
.flex-center {
display: flex;
align-items: center;
}
.btn {
border: 1px solid blue;
width: 60px;
text-align: center;
color: blue;
height: 33px;
line-height: 33px;
margin-left: 15px;
cursor: pointer;
}
/* 滑动控件容器,灰色背景 */
#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+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuNS1jMDIxIDc5LjE1NTc3MiwgMjAxNC8wMS8xMy0xOTo0NDowMCAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wTU09Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9tbS8iIHhtbG5zOnN0UmVmPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvc1R5cGUvUmVzb3VyY2VSZWYjIiB4bWxuczp4bXA9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC8iIHhtcE1NOk9yaWdpbmFsRG9jdW1lbnRJRD0ieG1wLmRpZDo0ZDhlNWY5My05NmI0LTRlNWQtOGFjYi03ZTY4OGYyMTU2ZTYiIHhtcE1NOkRvY3V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