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中@click绑定事件点击不生效的原因及解决方案

    vue中@click绑定事件点击不生效的原因及解决方案

    根据Vue2.0官方文档关于父子组件通讯的原则,父组件通过prop传递数据给子组件,子组件触发事件给父组件,这篇文章主要介绍了vue中@click绑定事件点击不生效的解决方案,需要的朋友可以参考下
    2022-12-12
  • Vuex中如何getters动态获取state的值

    Vuex中如何getters动态获取state的值

    这篇文章主要介绍了Vuex中如何getters动态获取state的值,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-08-08
  • vue3循环设置ref并获取的解决方案

    vue3循环设置ref并获取的解决方案

    我们在平时做业务的时候,父子组件通信会经常用到ref,这篇文章主要给大家介绍了关于vue3循环设置ref并获取的解决方案,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2024-02-02
  • VUE搭建分布式医疗挂号系统的前台预约挂号步骤详情

    VUE搭建分布式医疗挂号系统的前台预约挂号步骤详情

    这篇文章主要介绍了VUE搭建分布式医疗挂号系统的前台预约挂号步骤详情,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-04-04
  • Vue.js实现移动端短信验证码功能

    Vue.js实现移动端短信验证码功能

    现在的短信验证码一般为4位或6位,则页面中会相应的显示4个或6个文本框.好多网站需求都有此功能,今天小编给大家分享基于vue.js实现移动端短信验证码功能,需要的朋友参考下吧
    2017-03-03
  • vue 跳转页面$router.resolve和$router.push案例详解

    vue 跳转页面$router.resolve和$router.push案例详解

    这篇文章主要介绍了vue 跳转页面$router.resolve和$router.push案例详解,这样实现了既跳转了新页面,又不会让后端检测到页面链接不安全之类的,需要的朋友可以参考下
    2023-10-10
  • vue如何通过ref调用router-view子组件的方法

    vue如何通过ref调用router-view子组件的方法

    这篇文章主要介绍了vue 通过ref调用router-view子组件的方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧
    2023-11-11
  • Vue3 之 Vue 事件处理指南

    Vue3 之 Vue 事件处理指南

    Vue事件处理是每个Vue项目的必要方面。 它用于捕获用户输入,共享数据以及许多其他创造性方式。在本文中,会介绍基础知识,并提供一些用于处理事件的代码示例。需要的小伙伴可以参考下面文章的具体内容
    2021-09-09
  • vuex管理状态仓库使用详解

    vuex管理状态仓库使用详解

    这篇文章主要介绍了vuex管理状态仓库使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-07-07
  • 解决vue组件没显示,没起作用,没报错,但该显示的组件没显示问题

    解决vue组件没显示,没起作用,没报错,但该显示的组件没显示问题

    这篇文章主要介绍了解决vue组件没显示,没起作用,没报错,但该显示的组件没显示问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-09-09

最新评论