JavaScript实现滑动滑块验证登录功能详解
本文详细介绍如何使用原生JavaScript实现滑动滑块验证登录功能。通过HTML构建结构、CSS控制样式与层级,并结合鼠标事件(mousedown、mousemove、mouseup)计算位移量,实现滑块拖拽、验证成功判定及复位逻辑。代码包含完整的事件处理与状态管理,适用于前端交互开发参考。
滑动滑块验证登录功能概述
本文分享了一段使用JavaScript实现滑动滑块验证登录的完整代码示例,供读者参考学习。
HTML与CSS结构实现
1. HTML结构代码
<div class="box">
<!--滑块-->
<a href="#" rel="external nofollow" ><div class="btn">>></div></a>
<!--文字-->
<p class="text">拖动滑块验证</p>
<!--背景-->
<div class="bg"></div>
</div>
2. CSS样式定义
最外层容器需设置为相对定位,内部元素采用绝对定位。同时需合理设置z-index属性,确保滑块拖动时的层级显示正常。
.box{
position: relative;
width: 300px;
height: 34px;
background: #e8e8e8;
border-radius: 4px;
left: 20px;
}
.btn{
position: absolute;
top: 0;
width: 40px;
height:32px;
text-align: center;
line-height: 32px;
border-radius: 4px;
z-index: 3;
background-color: #fff;
border: 1px solid #ccc;
color: black;
}
.text{
position: absolute;
width: 100%;
margin: 0;
text-align: center;
line-height: 34px;
display: block;
z-index: 2;
/*-webkit-margin-before: 1em;
-webkit-margin-after: 1em;*/
}
.bg{
position: absolute;
height: 100%;
background-color: yellowgreen;
z-index: 1;
}
具体样式代码

JavaScript事件逻辑与代码实现
3. JavaScript事件逻辑
功能逻辑分析:用户按住滑块并拖动时,滑块随鼠标移动;若中途松开鼠标,滑块自动返回初始位置;若将滑块拖动至最右端,则验证成功,滑块停止移动。具体动作分解如下: 1. 按钮按下并发生位移。 2. 获取事件状态,通过event.clientX属性获取鼠标相对于X轴的距离。 3. 松开按钮时,滑块回到起始位置。 4. 验证结束时,松开按钮,滑块锁定且不可再次拖动。
1)初始化元素
var btn=document.querySelector(".btn");
var box=document.querySelector(".box");
var bg=document.querySelector(".bg");
var text=document.querySelector(".text");
或者使用封装的选择器函数
function $(name){
return document.querySelector(name);
};
var box=$(".box"),btn=$(".btn").....;
2)按下事件处理
按下后记录鼠标与X轴的初始距离
btn.onmousedown=function(e){
var downX=e.clientX;
3)拖动过程处理
拖动过程中,通过当前鼠标X轴距离减去初始距离,计算滑块的实际移动值。根据该移动值判断滑块是否可正常移动,以及是否已完成验证。
btn.onmousemove=function(e){
var moveX=e.clientX-downX;
// console.log(moveX);
//移动范围
if(moveX>-2){
this.style.left=moveX+"px";//将移动值赋值给滑块
bg.style.width=moveX+"px";//背景
if(moveX>=(box.offsetWidth-btn.offsetWidth)){//包含原始宽度内边距边框,不包含外边框
//拖到头,验证成功
flag=true;
text.innerHTML="验证成功";
text.style.color="white";
//事件清除
btn.onmousedown=null;
btn.onmousemove=null;
}
}
4)松开按钮处理
若未完成验证即松开,滑块回到原处并清除拖动状态
btn.onmouseup=function(){
//事件清除
btn.onmousemove=null;
if(flag)return;
this.style.left=0;//将移动值赋值给滑块
bg.style.width=0;//背景
}
4. 运行效果展示

5. 完整源码
//原生写法
window.onload=function(){
var btn=document.querySelector(".btn");
var box=document.querySelector(".box");
var bg=document.querySelector(".bg");
var text=document.querySelector(".text");
//封装选择器
// function $(name){
// return document.querySelector(name);
// };
// var box=$(".box"),btn=$(".btn").....;
var flag=false;
//按下onmousedown 拖动onmousemove
//document.querySelector(".btn").onmousedown=function(event){//event事件状态
// var e=event||window.event;
//获取方法集合,可直接通过id, 类, 类型, 属性, 属性值等来选取元素(返回此名字的第一个)。
btn.onmousedown=function(e){//按下
var downX=e.clientX; //按下后对x轴的距离
// console.log(downX);
// alert("1");
btn.onmousemove=function(e){//拖动
var moveX=e.clientX-downX; //拖动后与x轴距离减去初始值距离,移动值
// console.log(moveX);
//移动范围
if(moveX>-2){
this.style.left=moveX+"px";//将移动值赋值给滑块
bg.style.width=moveX+"px";//背景
if(moveX>=(box.offsetWidth-btn.offsetWidth)){//包含原始宽度内边距边框,不包含外边框
//拖到头,验证成功
flag=true;
text.innerHTML="验证成功";
text.style.color="white";
//事件清除
btn.onmousedown=null;
btn.onmousemove=null;
}
}
}
}
//松开按钮
btn.onmouseup=function(){
//事件清除
btn.onmousemove=null;
if(flag)return;
this.style.left=0;//将移动值赋值给滑块
bg.style.width=0;//背景
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持本站。
JS动态构建复杂嵌套JSON对象的方法
2026-11-05 15:41:52
JS负数取余为什么是负数?详解余数与模运算区别
2026-11-05 13:34:41
JS 解压出现乱码或报错怎么解决?
2026-11-05 12:32:38
JS 中 startsWith、indexOf 与正则判断字符串前缀的对比
2026-11-04 22:19:43
JS解析Excel后怎么将数据转换为JSON格式
2026-11-04 20:58:28
