JavaScript+html實現前端頁面滑動驗證(2)

本文實例為大傢分享瞭炫酷的前端頁面滑動驗證的具體代碼,供大傢參考,具體內容如下

分享炫酷的前端頁面滑動驗證

前面已經發過一種,這裡再發一種,隻是特效不一樣

還是直接上代碼:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <style>
        /* 滑動控件容器,灰色背景 */
        #dragContainer {
            position: relative;
            display: inline-block;
            background: #e8e8e8;
            width: 300px;
            height: 33px;
            border: 2px solid #e8e8e8;
        }
        /* 滑塊左邊部分,綠色背景 */
        #dragBg {
            position: absolute;
            background-color: #7ac23c;
            width: 0px;
            height: 100%;
        }
        /* 滑動驗證容器文本 */
        #dragText {
            position: absolute;
            width: 100%;
            height: 100%;
            /* 文字水平居中 */
            text-align: center;
            /* 文字垂直居中,這裡不能用百分比,因為百分比是相對原始line-height的,而非div高度 */
            line-height: 33px;
            /* 文本不允許選中 */
            user-select: none;
            -webkit-user-select: none;
        }
        /* 滑塊 */
        #dragHandler {
            position: absolute;
            width: 40px;
            height: 100%;
            cursor: move;
        }
        /* 滑塊初始背景 */
        .dragHandlerBg {
            background: #fff no-repeat center url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAA3hpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuNS1jMDIxIDc5LjE1NTc3MiwgMjAxNC8wMS8xMy0xOTo0NDowMCAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50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9d8WII4HOvk3ITkWJAXWUMlOoGQHmsE45ViQ2KuBuASoYC4Wf+OUYxz6mQkgwAAN9mIrUReCXgAAAABJRU5ErkJggg==");
        }
        /* 驗證成功時的滑塊背景 有√*/
        .dragHandlerOkBg {
            background: #fff no-repeat center url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAA3hpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuNS1jMDIxIDc5LjE1NTc3MiwgMjAxNC8wMS8xMy0xOTo0NDowMCAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wTU09Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9tbS8iIHhtbG5zOnN0UmVmPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvc1R5cGUvUmVzb3VyY2VSZWYjIiB4bWxuczp4bXA9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC8iIHhtcE1NOk9yaWdpbmFsRG9jdW1lbnRJRD0ieG1wLmRpZDo0ZDhlNWY5My05NmI0LTRlNWQtOGFjYi03ZTY4OGYyMTU2ZTYiIHhtcE1NOkRvY3VtZW50SUQ9InhtcC5kaWQ6NDlBRDI3NjVGMkQ2MTFFNEI5NDBCMjQ2M0ExMDQ1OUYiIHhtcE1NOkluc3RhbmNlSUQ9InhtcC5paWQ6NDlBRDI3NjRGMkQ2MTFFNEI5NDBCMjQ2M0ExMDQ1OUYiIHhtcDpDcmVhdG9yVG9vbD0iQWRvYmUgUGhvdG9zaG9wIENDIDIwMTQgKE1hY2ludG9zaCkiPiA8eG1wTU06RGVyaXZlZEZyb20gc3RSZWY6aW5zdGFuY2VJRD0ieG1wLmlpZDphNWEzMWNhMC1hYmViLTQxNWEtYTEwZS04Y2U5NzRlN2Q4YTEiIHN0UmVmOmRvY3VtZW50SUQ9InhtcC5kaWQ6NGQ4ZTVmOTMtOTZiNC00ZTVkLThhY2ItN2U2ODhmMjE1NmU2Ii8+IDwvcmRmOkRlc2NyaXB0aW9uPiA8L3JkZjpSREY+IDwveDp4bXBtZXRhPiA8P3hwYWNrZXQgZW5kPSJyIj8+k+sHwwAAASZJREFUeNpi/P//PwMyKD8uZw+kUoDYEYgloMIvgHg/EM/ptHx0EFk9I8wAoEZ+IDUPiIMY8IN1QJwENOgj3ACo5gNAbMBAHLgAxA4gQ5igAnNJ0MwAVTsX7IKyY7L2UNuJAf+AmAmJ78AEDTBiwGYg5gbifCSxFCZoaBMCy4A4GOjnH0D6DpK4IxNSVIHAfSDOAeLraJrjgJp/AwPbHMhejiQnwYRmUzNQ4VQgDQqXK0ia/0I17wJiPmQNTNBEAgMlQIWiQA2vgWw7QppBekGxsAjIiEUSBNnsBDWEAY9mEFgMMgBk00E0iZtA7AHEctDQ58MRuA6wlLgGFMoMpIG1QFeGwAIxGZo8GUhIysmwQGSAZgwHaEZhICIzOaBkJkqyM0CAAQDGx279Jf50AAAAAABJRU5ErkJggg==");
        }
    </style>
</head>
<body>
        <div id="dragContainer"><!-- 容器初始背景 -->
            <div id="dragBg"></div><!-- 綠色背景 -->
            <div id="dragText"></div><!-- 滑動容器文本 -->
            <div id="dragHandler" class="dragHandlerBg"></div>
        </div> <!--    滑塊         滑塊初始背景 -->
</body>

<script>
    //加載(事件會在頁面加載完成後觸發)
    window.onload = function() {
        //獲取滑動控件容器,灰色背景
        var dragContainer = document.getElementById("dragContainer");
        //獲取滑塊左邊部分,綠色背景
        var dragBg = document.getElementById("dragBg");
        //獲取滑動驗證容器文本
        var dragText = document.getElementById("dragText");
        //獲取滑塊
        var dragHandler = document.getElementById("dragHandler");

        //滑塊的最大偏移量                 =     滑動驗證容器文本長度                  -      滑塊長度
        var maxHandlerOffset = dragContainer.clientWidth - dragHandler.clientWidth;
        //是否驗證成功的標記
        var isVertifySucc = false;

        initDrag();

        function initDrag() {
            //在滑動驗證容器文本寫入“拖動滑塊驗證”
            dragText.textContent = "拖動滑塊驗證";
            //給滑塊添加鼠標按下監聽
            dragHandler.addEventListener("mousedown", onDragHandlerMouseDown);
        }

        //選中滑塊
        function onDragHandlerMouseDown() {
            //鼠標移動監聽
            document.addEventListener("mousemove", onDragHandlerMouseMove);
            //鼠標松開監聽
            document.addEventListener("mouseup",  onDragHandlerMouseUp);
        }

        //滑塊移動
        function onDragHandlerMouseMove() {
            /*
            html元素不存在width屬性,隻有clientWidth
            offsetX是相對當前元素的,clientX和pageX是相對其父元素的
            */
            //滑塊移動量
            var left = event.clientX - dragHandler.clientWidth / 2;
            //
            if(left < 0) {
                left = 0;
                //如果滑塊移動量   > 滑塊的最大偏移量 ,則調用驗證成功函數
            } else if(left > maxHandlerOffset) {
                left = maxHandlerOffset;
                verifySucc();
            }
            //滑塊移動量
            dragHandler.style.left = left + "px";
            //綠色背景的長度
            dragBg.style.width = dragHandler.style.left;
        }

        //松開滑塊函數
        function onDragHandlerMouseUp() {
            //移除鼠標移動監聽
            document.removeEventListener("mousemove", onDragHandlerMouseMove);
            //移除鼠標松開監聽
            document.removeEventListener("mouseup", onDragHandlerMouseUp);
            //初始化滑塊移動量
            dragHandler.style.left = 0;
            //初始化綠色背景
            dragBg.style.width = 0;
        }

        //驗證成功
        function verifySucc() {
            //成功標記,不可回彈
            isVertifySucc = false;
            //容器文本的文字改為白色“驗證通過”字體
            dragText.textContent = "驗證通過";
            dragText.style.color = "white";
            //驗證通過的滑塊背景
            dragHandler.setAttribute("class", "dragHandlerOkBg");
            //移除鼠標按下監聽
            dragHandler.removeEventListener("mousedown", onDragHandlerMouseDown);
            //移除 鼠標移動監聽
            document.removeEventListener("mousemove", onDragHandlerMouseMove);
            //移除鼠標松開監聽
            document.removeEventListener("mouseup", onDragHandlerMouseUp);
            // 匹配成功以後寫入你要跳轉的頁面
            //window.location.href="成功頁面.html" rel="external nofollow" ;
        };
    }
</script>
</html>

效果如下

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀: