温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Vue如何实现滑动验证功能

发布时间:2022-02-17 09:12:20 来源:亿速云 阅读:691 作者:小新 栏目:开发技术

这篇文章主要介绍了Vue如何实现滑动验证功能,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

用Vue实现滑动验证码,鼠标点击滑动验证,验证成功之后会显示验证通过。

程序分析

1、鼠标的点击
2、滑块的拖动
3、未验证之前滑动条上显示的文字
4、滑块箭头指向Vue函数
5、判断是否拖动完毕
6、拖动完毕时改变背景色并显示验证成功

效果图演示

原始状态

Vue如何实现滑动验证功能

点击之后拖动

Vue如何实现滑动验证功能

拖动完毕

Vue如何实现滑动验证功能

有没有感觉很奇妙的呢???

在欣赏代码之前准备工作要做好的!!!
**特别注意:**相关架包的引入(三个架包)

<script src="js/vue.js"></script>
<script src="https://unpkg.com/element-ui/lib/index.js"></script>
<script src="js/jquery.js"></script>

代码演示

<!DOCTYPE html>
<html>

    <head>
        <meta charset="UTF-8">
        <title></title>
        <script src="js/vue.js"></script>
        <script src="https://unpkg.com/element-ui/lib/index.js"></script>
        <script src="js/jquery.js"></script>
        <style>
            .big {
                border-radius:30px;
                position: relative;
                background-color: #75CDF9;
                width: 300px;
                height: 34px;
                margin-left: 30px;
                margin-top: 100px;
                line-height: 34px;
                text-align: center;
            }
            
            .hand {
                border-radius:30px;
                position: absolute;
                top: 0px;
                left: 0px;
                width: 40px;
                height: 32px;
                border: 1px solid #ccc;
                cursor: move;
            }
            
            .handler {
                background: #fff url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAA3hpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD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vcmRmOkRlc2NyaXB0aW9uPiA8L3JkZjpSREY+IDwveDp4bXBtZXRhPiA8P3hwYWNrZXQgZW5kPSJyIj8+YiRG4AAAALFJREFUeNpi/P//PwMlgImBQkA9A+bOnfsIiBOxKcInh+yCaCDuByoswaIOpxwjciACFegBqZ1AvBSIS5OTk/8TkmNEjwWgQiUgtQuIjwAxUF3yX3xyGIEIFLwHpKyAWB+I1xGSwxULIGf9A7mQkBwTlhBXAFLHgPgqEAcTkmNCU6AL9d8WII4HOvk3ITkWJAXWUMlOoGQHmsE45ViQ2KuBuASoYC4Wf+OUYxz6mQkgwAAN9mIrUReCXgAAAABJRU5ErkJggg==") no-repeat center;
            }
            
            .handlerFinish {
                background: #fff url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAA3hpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh7cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuNS1jMDIxIDc5LjE1NTc3MiwgMjAxNC8wMS8xMy0xOTo0NDowMCAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wTU09Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9tbS8iIHhtbG5zOnN0UmVmPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvc1R5cGUvUmVzb3VyY2VSZWYjIiB4bWxuczp4bXA9Imh0dHA6Ly9ucy5hZG9iZS5jb20ve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AAABJRU5ErkJggg==") no-repeat center;
            }
            
            .bg {
                border-radius:30px;
                background-color: #13CE66;
                height: 34px;
                width: 0px;
            }
            
            .text {
                position: absolute;
                top: 0px;
                width: 300px;
                -moz-user-select: none;
                -webkit-user-select: none;
                user-select: none;
                -o-user-select: none;
                -ms-user-select: none;
            }
        </style>

    </head>

    <body>

        <div id="app">
            <div class="big" >
                <div class="bg"></div>
                <div class="text">{{confirmWords}}</div>
                <div @mousedown="mousedownFn($event)" class="hand handler"></div>
            </div>

        </div>
        
        
    </body>
    
    <script>
        var vm = new Vue({
    
            el: "#app",
            name: '',
            components: {},
            props: {},
            data() {
                return {
                    beginClientX: 0,
                    mouseMoveStata: false,
                    maxwidth: 258,
                    confirmWords: '拖动滑块验证',
                    /*滑块文字*/
                    confirmSuccess: false, /*是否成功*/
                }
            },
            created() {},
            watch: {
            },
            methods: {
                mousedownFn: function(e) {
                    this.mouseMoveStata = true;
                    this.beginClientX = e.clientX;
                }, 
                //按下滑块
                successFunction() {
                    $(".hand").removeClass('handler').addClass('handlerFinish');
                    this.confirmWords = '验证通过'
                    $(".big").css({
                        'color': '#fff'
                    });
                    $(".big").css({
                        'background-color': '#13CE66'
                    });
                    $(".hand").css({
                        'left': this.maxwidth
                    });
                    $(".bg").css({
                        'width': this.maxwidth
                    });
                    $('body').unbind('mousemove');
                    $('body').unbind('mouseup');
                    this.confirmSuccess = true;
                    
                } //判断是否成功
            },
            mounted() {
                $('body').on('mousemove', (e) => {
                    //拖动使用箭头指向Vue函数
                    if(this.mouseMoveStata) {
                        var width = e.clientX - this.beginClientX;
                        if(width > 0 && width <= this.maxwidth) {
                            $(".hand").css({
                                'left': width
                            });
                            $(".bg").css({
                                'width': width
                            });
                        } else if(width > this.maxwidth) {
                            this.successFunction();
                        }
                    }
                });
                $('body').on('mouseup', (e) => {
                    //鼠标放开 
                    this.mouseMoveStata = false;
                    var width = e.clientX - this.beginClientX;
                    if(width < this.maxwidth) {
                        $(".hand").css({
                            'left': 0
                        });
                        $(".bg").css({
                            'width': 0
                        });
                    }
                })
            }
        });
    </script>

</html>

感谢你能够认真阅读完这篇文章,希望小编分享的“Vue如何实现滑动验证功能”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

vue
AI