JavaScript实现滑动滑块验证登录功能详解

作者:纸飞机时间:2026-10-23 19:59:09浏览:616

本文详细介绍如何使用原生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;//背景
 
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持本站。

作者最新文章