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

作者:闭包旅人

时间:2026-10-24

浏览:271

本文详细介绍如何使用 JavaScript 实现前端登录滑块验证功能。通过监听鼠标按下、移动和释放事件,计算滑块位移距离,判断是否达到验证阈值。文章包含完整的 HTML 结构、CSS 样式及核心 JS 逻辑代码,帮助开发者快速集成安全验证模块。

文章目录

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

本文分享了一段使用 JavaScript 编写的登录滑块验证完整代码,供读者参考学习。

HTML 结构代码

首先是 HTML 结构部分的代码实现。


<div class="login-select">
    <div v-show="errselectFlag" id="err-select"></div>
        <p id="title-p">按住滑块,拖拽验证</p>
        <div id="left-select"></div>
        <div id="right-select">
        <i id="icon-dui" class="iconfont icon-right"></i>
    </div>
</div>

JavaScript 核心逻辑

接下来是核心的 JavaScript 逻辑代码,用于处理用户交互。


//   滑块验证
var oRight = document.getElementById("right-select");
var bg = document.getElementById("left-select");
var title = document.getElementById("title-p");
var i = document.getElementById("icon-dui");
    oRight.onmousedown = function (e) {
      var downX = e.clientX; //按下按钮后与窗口的x轴间距
      // 鼠标移动事件
      oRight.onmousemove = function (e) {
        if (e.clientX != 240) {
          oRight.style.left = 0 + "px";
          bg.style.left = 0 + "px";
        }
        var moveX = e.clientX - downX; //滑动的时候距离窗口的x轴的间距
        //只有在大于0的时候才拖动,否则会出现反向拖动
        if (moveX > 0) {
          oRight.style.left = moveX + "px"; //滑块与左边的距离
          bg.style.width = moveX + "px"; //背景的宽度就是滑块距离左边的位置
          if (moveX >= 280 - oRight.offsetWidth) {
            i.className = "iconfont icon-xingzhuang";
            i.style.color = "rgb(86, 192, 15)";
            title.innerText = "验证通过";
            title.style.color = "#fff";
            oRight.onmousemove = null;
            oRight.onmousedown = null;
          }
        }
      };
    };

CSS 样式代码

然后是 CSS 样式代码,负责滑块的外观与布局。

注意:示例中的样式文件使用了 Sass 预处理器进行编写。


*{margin: 0;padding: 0;box-sizing: border-box;}
    .login-select {
      width: 280px;
      height: 40px;
      margin: auto;
      margin-top: 20px;
      margin-left: 15px;
      margin-right: 15px;
      text-align: center;
      line-height: 40px;
      background: rgba(134, 134, 131, 0.6);
      display: flex;
      position: relative;
      #err-select {
        width: 138px;
        height: 38px;
        position: absolute;
        right: -152px;
        top: 0;
        color: #fff;
        font-size: 12px;
        border-radius: 5px;
        line-height: 38px;
        text-align: center;
        background: rgb(177, 71, 71);
      }
      #title-p {
        text-align: center;
        line-height: 40px;
        width: 100%;
        height: 100%;
        font-size: 14px;
        position: absolute;
      }
      #left-select {
        width: 0;
        height: 100%;
        transform: translate(0.3s);
        background: rgb(86, 192, 15);
      }
      #right-select {
        width: 40px;
        height: 40px;
        background: #fff;
        color: #aaaa;
        text-align: center;
        line-height: 40px;
        border: 1px solid #ccc;
        position: absolute;
        cursor: move;
      }
    }

以上就是本文介绍的全部内容,希望这段代码能对各位的学习有所帮助,同时也欢迎大家继续支持本站获取更多技术资源。

实用工具推荐

电子包浆

绿化、压缩、噪点和虚拟水印,自由调节做旧程度。

使用 ›
图片压缩

选择画质和输出格式,预览压缩前后的大小。

使用 ›
相关文章