vue滑动解锁组件使用方法详解

 更新时间:2022年03月03日 11:49:20   作者:qq_34096214  
这篇文章主要为大家详细介绍了vue滑动解锁组件的使用方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了vue滑动解锁组件的使用,供大家参考,具体内容如下

这是一个pc端的滑动解锁组件

效果图:

话不多说,直接上代码

html部分

<template>
    <div class="dragVerify">
        <div class="spout" ref="spout">
            <div
                class="slidingBlock"
                ref="slidingBlock"
                :style="{ left: `${place}%` }"
                @mousedown="mousedown($event)"
                :class="place < distance ? 'unfinished' : 'complete'"
            ></div>
            <div class="succeedBox" :class="place >= distance ? 'succeedText' : ''" :style="{ width: `${place}%` }"></div>
        </div>
    </div>
</template>

js部分

export default {
    name: 'dragVerify',
    data() {
        return {
            place: 0,
            sliding: {
                isDown: true,
                X: 0 // 初始X值
            },
            move: {
                X: 0 // 移动X值
            },
            spoutW: 0,
            slidingBlockW: 0,
            distance: 1 // 要移动的距离
        }
    },
    mounted() {},
    methods: {
        // 鼠标事件
        mousedown(e) {
            if (this.place < this.distance) {
                this.sliding.isDown = true
                // 计算百分比
                this.spoutW = this.$refs.spout.offsetWidth
                this.slidingBlockW = this.$refs.slidingBlock.offsetWidth
                this.distance = 100 - (this.slidingBlockW / this.spoutW) * 100
            }
            this.sliding.X = e.x
            // 添加鼠标的移动事件
            document.addEventListener('mousemove', e => {
                if (this.sliding.isDown) {
                    this.move.X = e.x
                    if (this.place >= this.distance) {
                        this.place = this.distance
                    } else if (this.place >= 0 && this.place < this.distance) {
                        this.place = ((this.move.X - this.sliding.X) / this.spoutW) * 100
                    }
                    if (this.place <= 0) {
                        this.place = 0
                        document.removeEventListener('mousemove', null, false)
                        return
                    }
                }
                e.preventDefault()
            })
            // 松开鼠标
            document.onmouseup = e => {
                if (this.place == this.distance) {
                    this.$emit('setVerify', true)
                } else {
                    this.sliding.isDown = false
                    this.place = 0
                    this.$emit('setVerify', false)
                }
            }
        }
    }
}

css部分

.dragVerify {
    border: 1px solid #e1e1e1;
    height: 40px;
    background: #eeeeee;
}
.spout {
    line-height: 40px;
    height: 40px;
    /* text-align: center; */
    position: relative;
    width: 293px;
}
.spout::before {
    content: '请按住滑块,拖动到最右边';
    width: 233px;
    top: 0;
    right: 0;
    bottom: 0;
    color: #919593;
    font-size: 16px;
    text-align: center;
    position: absolute;
}
.succeedText::before {
    content: '验证通过';
    width: 233px;
    top: 0;
    right: 0;
    bottom: 0;
    color: #ffffff;
    font-size: 16px;
    text-align: center;
    position: absolute;
}
.succeedBox {
    color: #ffffff;
    font-size: 16px;
    text-align: center;
    line-height: 38px;
    height: 38px;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: #23b14d;
    z-index: 8;
}
.slidingBlock {
    width: 60px;
    /* height: 38px; */
    height: calc(100% - 0.1rem);
    border: 1px solid #e1e1e1;
    border-top: none;
    /* border-bottom: none; */
    border-left: none;
    background: #ffffff;
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    /* margin-left: -1px; */
    cursor: move;
    z-index: 9;
}
.slidingBlock::after {
    content: '';
    position: absolute;
    background-size: 100% 100%;
    background-repeat: no-repeat;
    width: 20px;
    height: 20px;
    left: 50%;
    top: 50%;
    margin-left: -10px;
    margin-top: -10px;
}
.unfinished::after {
    background-image: url(你的图片);
}
.complete::after {
    background-image: url(你的图片);
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • 前端VUE双语实现方案详细教程

    前端VUE双语实现方案详细教程

    在项目需求中我们会遇到国际化的中英文切换,这篇文章主要给大家介绍了关于前端VUE双语实现方案的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2024-08-08
  • Vue全局监测错误并生成错误日志实现方法介绍

    Vue全局监测错误并生成错误日志实现方法介绍

    在做完一个项目后,之后的维护尤为重要。这时,如果项目配置了错误日志记录,这样能大大减少维护难度。虽然不一定能捕获到全部的错误,但是一般的错误还是可以监测到的。这样就不用测试人员去一遍一遍复现bug了
    2022-10-10
  • vue filters和directives访问this的问题详解

    vue filters和directives访问this的问题详解

    这篇文章主要介绍了vue filters和directives访问this的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-01-01
  • vsCode安装配置创建Vue3项目全过程

    vsCode安装配置创建Vue3项目全过程

    本文介绍了如何在Windows系统上安装和配置Vue.js开发环境,包括安装VS Code、Node.js、Vue CLI以及创建和启动一个Vue项目
    2025-01-01
  • 详解Vue组件实现tips的总结

    详解Vue组件实现tips的总结

    这篇文章主要介绍了详解Vue组件实现tips的总结,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-11-11
  • Vuex报错之[vuex] unknown mutation type: handlePower问题及解决

    Vuex报错之[vuex] unknown mutation type: han

    这篇文章主要介绍了Vuex报错之[vuex] unknown mutation type: handlePower问题及解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-07-07
  • Delete `,` 如何解决(vue eslint与prettier冲突)

    Delete `,` 如何解决(vue eslint与prettier冲突)

    这篇文章主要介绍了Delete `,` 如何解决(vue eslint与prettier冲突)问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-10-10
  • vue实现列表滑动下拉加载数据的方法

    vue实现列表滑动下拉加载数据的方法

    文章介绍了如何使用Vue实现列表滑动下拉加载数据的功能,通过监听滚动事件,检测用户是否滚动到底部,然后动态加载更多数据,附带了实现思路和案例代码,感兴趣的朋友一起看看吧
    2024-11-11
  • Vue.js使用computed属性实现数据自动更新

    Vue.js使用computed属性实现数据自动更新

    在Vue组件中,computed属性是在组件的选项对象中声明的,你可以把它们想象成组件的一个小功能,告诉Vue当某些数据变化时,如何更新界面,本文给大家介绍了Vue.js使用computed属性实现数据自动更新,需要的朋友可以参考下
    2024-06-06
  • elementUI组件中el-date-picker限制时间范围精确到小时的方法

    elementUI组件中el-date-picker限制时间范围精确到小时的方法

    现在需要做一个时间选择器,可以根据小时(同时选天和小时)和天 和月,节假日等类型控制日历的选择样式,下面这篇文章主要给大家介绍了关于elementUI组件中el-date-picker限制时间范围精确到小时的相关资料,需要的朋友可以参考下
    2023-04-04

最新评论