JS实现简单的滑块验证码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<style>
    .verify_box{
        width: 240px;
        height: 40px;
        background: #e9e6e9;
        position: relative;
    }
    .verify_box p{
        position: absolute;
        top:-20%;
        left: 45%;
        z-index: 5;
    }
    .shadow {
        position: absolute;
        height: 40px;
        background: greenyellow;
    }
    .verify_core{
        background: #fff;
        border: 1px solid #adadc2;
        width: 50px;
        height: 40px;
        box-sizing: border-box;
        position: absolute;
        z-index: 20;
        background-image: url('./toRight.png');
        background-size: 100% 100%;
    }
    .verify_core img{
        width: 30px;
        height: 30px;
        margin: 5px auto;
        display: block;
    }
</style>
<body>
    <div class="verify_box">
        <!-- 滑块 -->
        <div class="verify_core">
        </div>
        <!-- 文本 -->
        <p>向右滑动</p>
        <!-- 被滑过的部分 -->
        <div class="shadow">
        </div>
    </div>

</body>
<script>
    var verify_box = document.querySelector('.verify_box'); 
    var text = verify_box.querySelector('p'); 
    var shadow = verify_box.querySelector('.shadow');  
    var verify_core = verify_box.querySelector('.verify_core');
    var isSuccess = false;   //检查是否成功
    verify_core.onmousedown = function(event) {   //按下滑块
        var e = event || window.event;
        var downX = e.clientX;        
        verify_core.onmousemove = function(event) {     //移动滑块
            var moveX = event.clientX - downX;
            if(moveX > 0) {
                shadow.style.width = moveX + 'px';
                this.style.left =  moveX + 'px';
                if(moveX >= (verify_box.offsetWidth - this.offsetWidth)) {   
                    text.innerText = "通过";
                    isSuccess = true;
                    verify_core.style.backgroundImage = "url('./true.png')";
                    this.onmousemove = null;   //当通过时,清除鼠标移动事件
                    this.onmousedown = null;    //当通过时,清除鼠标按下事件
                }
            }
            
        }
    }

    verify_core.onmouseup = function(event) {
        this.onmousemove = null;  
        if(isSuccess) return;   //如果成功,直接退出
        this.style.left = 0;
        shadow.style.width = 0;
    }
</script>
</html>

效果如下: 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值