当前位置:首页 > VUE

vue实现滑动解锁

2026-03-09 05:00:14VUE

vue实现滑动解锁功能

滑动解锁是一种常见的交互方式,通常用于验证用户操作。以下是基于Vue实现滑动解锁的几种方法:

使用原生Vue实现

创建滑动解锁组件需要处理触摸事件和滑动逻辑:

<template>
  <div class="slider-container">
    <div class="slider-track">
      <div 
        class="slider-thumb"
        :style="{ left: thumbPosition + 'px' }"
        @mousedown="startDrag"
        @touchstart="startDrag"
      ></div>
    </div>
    <div class="slider-text">{{ isUnlocked ? '解锁成功' : '滑动解锁' }}</div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isDragging: false,
      startX: 0,
      thumbPosition: 0,
      maxPosition: 300,
      isUnlocked: false
    }
  },
  methods: {
    startDrag(e) {
      this.isDragging = true
      this.startX = e.type.includes('touch') ? e.touches[0].clientX : e.clientX
      document.addEventListener('mousemove', this.onDrag)
      document.addEventListener('touchmove', this.onDrag, { passive: false })
      document.addEventListener('mouseup', this.stopDrag)
      document.addEventListener('touchend', this.stopDrag)
    },
    onDrag(e) {
      if (!this.isDragging) return
      e.preventDefault()

      const clientX = e.type.includes('touch') ? e.touches[0].clientX : e.clientX
      const moveX = clientX - this.startX

      this.thumbPosition = Math.min(
        Math.max(0, moveX),
        this.maxPosition
      )

      if (this.thumbPosition === this.maxPosition) {
        this.isUnlocked = true
        this.stopDrag()
      }
    },
    stopDrag() {
      this.isDragging = false
      if (!this.isUnlocked) {
        this.thumbPosition = 0
      }
      document.removeEventListener('mousemove', this.onDrag)
      document.removeEventListener('touchmove', this.onDrag)
      document.removeEventListener('mouseup', this.stopDrag)
      document.removeEventListener('touchend', this.stopDrag)
    }
  }
}
</script>

<style>
.slider-container {
  width: 350px;
  margin: 20px auto;
}
.slider-track {
  height: 40px;
  background: #eee;
  border-radius: 20px;
  position: relative;
}
.slider-thumb {
  width: 40px;
  height: 40px;
  background: #42b983;
  border-radius: 50%;
  position: absolute;
  top: 0;
  left: 0;
  cursor: pointer;
}
.slider-text {
  text-align: center;
  margin-top: 10px;
}
</style>

使用第三方库vue-drag-drop

安装vue-drag-drop库:

npm install vue-drag-drop

实现代码:

vue实现滑动解锁

<template>
  <div class="slider-wrapper">
    <div class="slider-track" ref="track">
      <div 
        class="slider-thumb"
        v-draggable="dragOptions"
        :style="{ left: thumbPosition + 'px' }"
      ></div>
    </div>
    <div class="slider-text">{{ isUnlocked ? '验证通过' : '请滑动滑块' }}</div>
  </div>
</template>

<script>
import { draggable } from 'vue-drag-drop'

export default {
  directives: { draggable },
  data() {
    return {
      thumbPosition: 0,
      maxPosition: 300,
      isUnlocked: false,
      dragOptions: {
        onDrag: this.handleDrag,
        onEnd: this.handleDragEnd
      }
    }
  },
  methods: {
    handleDrag({ positionDiff }) {
      this.thumbPosition = Math.min(
        Math.max(0, this.thumbPosition + positionDiff.left),
        this.maxPosition
      )
    },
    handleDragEnd() {
      if (this.thumbPosition === this.maxPosition) {
        this.isUnlocked = true
      } else {
        this.thumbPosition = 0
      }
    }
  }
}
</script>

实现进阶功能

  1. 添加动画效果

    .slider-thumb {
    transition: left 0.3s ease-out;
    }
  2. 添加验证回调

    vue实现滑动解锁

    props: {
    onSuccess: {
     type: Function,
     default: () => {}
    }
    },
    methods: {
    handleDragEnd() {
     if (this.thumbPosition === this.maxPosition) {
       this.isUnlocked = true
       this.$emit('success')
       this.onSuccess()
     }
    }
    }
  3. 重置功能

    methods: {
    reset() {
     this.thumbPosition = 0
     this.isUnlocked = false
    }
    }

移动端优化

  1. 添加touch事件支持

    mounted() {
    this.$refs.track.addEventListener('touchmove', this.preventScroll, { passive: false })
    },
    methods: {
    preventScroll(e) {
     if (this.isDragging) {
       e.preventDefault()
     }
    }
    }
  2. 响应式设计

    @media (max-width: 768px) {
    .slider-container {
     width: 90%;
    }
    .slider-track {
     height: 35px;
    }
    .slider-thumb {
     width: 35px;
     height: 35px;
    }
    }

这些实现方式可以根据具体需求进行调整和扩展,创建出符合项目风格的滑动解锁组件。

标签: 解锁vue
分享给朋友:

相关文章

vue实现cs

vue实现cs

Vue 实现 CS(Client-Side)渲染的常见方法 Vue.js 是一个用于构建用户界面的渐进式框架,默认采用客户端渲染(CSR)。以下是几种常见的实现方式: 使用 Vue CLI 创建项目…

实现vue table

实现vue table

Vue 表格实现方法 基础表格实现 使用 Vue 的 v-for 指令可以快速渲染表格数据。在模板中定义表格结构,通过数据绑定动态生成行和列。 <template> <tabl…

vue实现 hover

vue实现 hover

实现 Vue 中的 hover 效果 在 Vue 中实现 hover 效果可以通过多种方式完成,以下是常见的几种方法: 使用 CSS 伪类 最简单的方式是直接使用 CSS 的 :hover 伪类,无…

vue实现mvvm

vue实现mvvm

Vue 实现 MVVM 模式 MVVM(Model-View-ViewModel)是一种前端架构模式,Vue 是其典型实现之一。MVVM 的核心是通过数据绑定和响应式系统实现视图与数据的自动同步。…

vue实现甘特图

vue实现甘特图

使用 Vue 实现甘特图 基于开源库(如 vue-ganttastic) 安装依赖库: npm install vue-ganttastic 示例代码: <template> &l…

vue实现微云

vue实现微云

Vue 实现微云功能 使用 Vue 实现类似微云的网盘功能需要结合前端框架、后端存储及文件处理技术。以下是关键实现步骤和代码示例: 前端框架搭建 使用 Vue CLI 创建项目基础结构,安装必要依赖…