前端-滑動驗證碼

前言

趁著業(yè)余時間,簡單實現(xiàn)了滑動驗證碼。本文章主基于JQuery實現(xiàn)了滑動驗證碼的功能。主要是鼠標(biāo)按下松開的時候計算并設(shè)置滑塊的位置,其他都是一些樣式,另外用了下JQuery的動畫$.animate()。

代碼實現(xiàn)

注意事項??:修改圖片路徑 如果cdn太慢需下載到本地 圖片大小需要464*174、可以自己按需要修改 主要結(jié)合background-position屬性分隔區(qū)塊

<!DOCTYPE html>
<html>
<head>
    <title>滑動驗證碼</title>
    <link rel="stylesheet" >
</head>
<style type="text/css">
    * {padding: 0;margin: 0;}
    .subject{background: url('../image/yanzhengmatest.jpg') no-repeat;width: 464px;height:174px;display: inline-block;position: relative;}
    .round-rect{position: absolute;background: white;width: 80px;height: 40px;top: 150px;z-index:999;border-radius:12px; }
    .round-excute{background: url('../image/yanzhengmatest.jpg');width: 80px;height: 40px;position: absolute;border-radius:12px;z-index:9999;top: 150px;overflow: hidden;left: 10px;}
    .scroll{background: #dedede;width: 464px;height: 30px;position: relative;border-radius: 5px;}
    .huakuai{background: white;height: 30px;width: 80px;position: absolute;left: 10px;border-radius: 5px;overflow: hidden;}
    .bar{background: #5b91f6;height: 30px;width: 10px;position: absolute;}
    .subject .success{width: 100px;height: 40px;border-radius: 5px;border:2px solid #efefef;background: #efefff;display: inline-block;left:180px;top: 50px;position: absolute;display: none;}
    .subject    .success span.glyphicon-ok{color: green;font-size: 20px;}
    .subject    .success .round{width: 30px;height: 30px;display: inline-block;background: #4491f1;border-radius: 50%;text-align: center;}
</style>
<body>
    <center>
        <div class="subject">
            <div class="round-excute"></div>
            <div class="round-rect"></div>
            <div class="success">
                <span class="round">        
                <span class="glyphicon glyphicon-ok"></span></span>驗證成功
            </div>
        </div>
        <!-- <div style="background:url('../image/yanzhengmatest.jpg'); background-position: 0px 0px;width: 80px;height: 40px;">
        </div> -->
        <div class="scroll">
            <div class="huakuai"></div>
            <div class="bar"></div>
        </div>
        </center>
</body>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
    $(function(){
        var range = 5;
        //var initTop = init();
        var initTop = init();
        var initposition = {'x':0,'y':0};
        var initx=$('.subject').offset().left;
        
        //鼠標(biāo)按在滑塊上
        $('.huakuai').bind('mousedown',function(e){
            initposition.x=e.offsetX;
            initposition.y=e.offsetY;
             $('.huakuai').bind('mousemove',function(e){
             
                $(this).css('left',Math.min(Math.max(10,e.clientX-initx-10-initposition.x),$('.scroll').width()-$('.huakuai').width())+'px');
                $('.round-excute').css('left',Math.min(Math.max(10,e.clientX-initx-10-initposition.x),$('.scroll').width()-$('.huakuai').width())+'px');
                $('.scroll .bar').css('width',Math.min(Math.max(10,e.clientX-initx-10-initposition.x),$('.scroll').width()-$('.huakuai').width())+'px');
            }); 
        }); 

        //鼠標(biāo)按下之后松開
        $(window).bind('mouseup',function(){
             //松開之后需要移除鼠標(biāo)移動的監(jiān)聽事件,不然滑塊還會繼續(xù)動
             $('.huakuai').unbind('mousemove');
             //range是誤差范圍,因為拼圖塊和拼圖區(qū)域大小一樣,所以重合之后left和top不應(yīng)該相差太遠(yuǎn),自己定義范圍
             if(!(abs($('.round-rect').offset().left-$('.round-excute').offset().left)>range || abs($('.round-rect').offset().top-$('.round-excute').offset().top)>range )){
                $('.subject .success').show();
             }
             else {
                //沒對齊松手后還原位置
                $('.huakuai').animate({'left':'10px'},'fast');
                $('.round-excute').animate({'left':'10px','top':initTop},'fast');
                $('.scroll .bar').animate({'width' : 10+'px'},'fast');
             }

        });

        //隨機(jī)位置初始化
        function init(){
            var randx = ($('.subject').width()-$('.round-excute').width()-20)*Math.random()+10;//拼圖區(qū)域和塊出現(xiàn)范圍,-20和=10是為了間隙
            var randy = ($('.subject').height()-$('.round-excute').height()-20)*Math.random()+10;
            $('.round-rect').css('left',randx+'px');
            $('.round-rect').css('top',randy+'px');
            console.log(randx+','+randy);
            $('.round-excute').css('background-position',-randx+'px '+(-randy)+'px')
            console.log(-randx+'px '+(-randy)+'px');
            $('.round-excute').css('top',randy+'px');
            return randy;
        }

        //絕對值函數(shù)
        function abs(x){
            return x>=0?x:-x;   
        }

        
    });
</script>
</html>

感覺復(fù)用性方面還是太差了,沒有將其組件化,告誡自己下次做事情要做到極致。
PS:另外滑動驗證碼也不是特別安全,需要結(jié)合手機(jī)驗證碼一起使用

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 作者簡介 原創(chuàng)微信公眾號郭霖 WeChat ID: guolin_blog 本篇來自 老老司機(jī)(第六篇了)張旭童的...
    木木00閱讀 3,216評論 0 15
  • 瑪麗婭·索特斯科娃在這個司機(jī)從沒有失敗過,希望她可以進(jìn)入冬奧會國家隊。來自選手的教練葉蓮娜·布雅諾娃。 “誰參加冬...
    云游四方的旅人閱讀 326評論 0 0
  • 素材:來自武漢晚報 9月9日是武漢軟件工程技術(shù)學(xué)院新生報名日,19歲的大一新生童浩提前一天向?qū)W校提出了申請,要求在...
    羽衣甘蘭閱讀 240評論 0 0
  • 糧票是我國特定歷史條件下的產(chǎn)物,是計劃經(jīng)濟(jì)時代的一種購糧憑證,退出經(jīng)濟(jì)舞臺后,糧票便進(jìn)入了收藏市場,是可遇不可求的...
    藝術(shù)人生88閱讀 414評論 0 0

友情鏈接更多精彩內(nèi)容