vue驗證滑塊組件推薦


今天做登錄頁面時要做一個驗證滑塊的功能。

因為不想去寫樣式引用了組件。在網上找了很久,個人認為可以的一款組件

<template>
    <div class="drag" ref="dragDiv">
        <div class="drag_bg"></div>
        <div class="drag_text">{{ confirmWords }}</div>
        <div
            ref="moveDiv"
            @mousedown="mouseDownFn($event)"
            :class="{ handler_ok_bg: confirmSuccess }"
            class="handler handler_bg"
            style="position: absolute; top: 0px; left: 0px;"
        ></div>
    </div>
</template>

<script>
export default {
    data() {
        return {
            beginClientX: 0 /*距離屏幕左端距離*/,
            mouseMoveState: false /*觸發拖動狀態  判斷*/,
            maxWidth: "" /*拖動最大寬度,依據滑塊寬度算出來的*/,
            confirmWords: "請按住滑塊,拖動到最右邊" /*滑塊文字*/,
            confirmSuccess: false /*驗證成功判斷*/
        };
    },
    methods: {
        //mousedown 事件
        mouseDownFn: function(e) {
            if (!this.confirmSuccess) {
                e.preventDefault && e.preventDefault(); //阻止文字選中等 瀏覽器默認事件
                this.mouseMoveState = true;
                this.beginClientX = e.clientX;
            }
        },
        //驗證成功函數
        successFunction() {
            this.$store.commit("statestatus", true);
            this.confirmSuccess = true;
            this.confirmWords = "驗證通過";
            if (window.addEventListener) {
                document
                    .getElementsByTagName("html")[0]
                    .removeEventListener("mousemove", this.mouseMoveFn);
                document
                    .getElementsByTagName("html")[0]
                    .removeEventListener("mouseup", this.moseUpFn);
            } else {
                document
                    .getElementsByTagName("html")[0]
                    .removeEventListener("mouseup", () => {});
            }
            document.getElementsByClassName("drag_text")[0].style.color =
                "#fff";
            document.getElementsByClassName("handler")[0].style.left =
                this.maxWidth + "px";
            document.getElementsByClassName("drag_bg")[0].style.width =
                this.maxWidth + "px";
        },
        //mousemove事件
        mouseMoveFn(e) {
            if (this.mouseMoveState) {
                let width = e.clientX - this.beginClientX;
                if (width > 0 && width <= this.maxWidth) {
                    document.getElementsByClassName("handler")[0].style.left =
                        width + "px";
                    document.getElementsByClassName("drag_bg")[0].style.width =
                        width + "px";
                } else if (width > this.maxWidth) {
                    this.successFunction();
                }
            }
        },
        //mouseup事件
        moseUpFn(e) {
            this.mouseMoveState = false;
            var width = e.clientX - this.beginClientX;
            if (width < this.maxWidth) {
                document.getElementsByClassName("handler")[0].style.left =
                    0 + "px";
                document.getElementsByClassName("drag_bg")[0].style.width =
                    0 + "px";
            }
        }
    },
    mounted() {
        this.maxWidth =
            this.$refs.dragDiv.clientWidth - this.$refs.moveDiv.clientWidth;
        document
            .getElementsByTagName("html")[0]
            .addEventListener("mousemove", this.mouseMoveFn);
        document
            .getElementsByTagName("html")[0]
            .addEventListener("mouseup", this.moseUpFn);
    }
};
</script>
<style scoped>
.drag {
    position: relative;
    background-color: #ebebeb;
    width: 100%;
    height: 42px;
    line-height: 42px;
    text-align: center;
    border: 0px solid #ccc;
    border-radius: 4px;
}
.handler {
    width: 69px;
    height: 42px;
    border: 1px solid #ebebeb;
    border-radius: 4px;
    cursor: move;
}
.handler_bg {
    background: #fff
        url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAA3hpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuNS1jMDIxIDc5LjE1NTc3MiwgMjAxNC8wMS8xMy0xOTo0NDowMCAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wTU09Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuM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_ok_bg {
    border-top-left-radius: 0;
    border-bottom-left-radius: 0;
    background: #fff url("~@/assets/img/success.png") no-repeat center;
    background-size: 20px 20px;
}
.drag_bg {
    background-color: #0094ff;
    height: 42px;
    width: 0px;
    border-radius: 4px 0 0 4px;
}
.drag_text {
    position: absolute;
    top: 0px;
    width: 100%;
    text-align: center;
    -moz-user-select: none;
    -webkit-user-select: none;
    user-select: none;
    -o-user-select: none;
    -ms-user-select: none;
}
</style>

 樣式如下:

 

 

 

 

 因為要做一個驗證通過后馬上顯示驗證碼的輸入框。要做一個判斷。但是發現出發的事件要在組件內才行。

這里我用了vuex,存儲一個狀態。然后在組件中獲取,並返回vuex中state一個狀態。

this.$store.commit("statestatus", true);


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM