当前位置:首页 > VUE

vue实现滑动验证

2026-02-20 10:44:07VUE

Vue 滑动验证实现方法

使用 vue-drag-verify 组件

vue-drag-verify 是一个轻量级的 Vue 滑动验证组件,安装简单且可定制化程度高。

安装依赖:

npm install vue-drag-verify --save

基本用法:

<template>
  <drag-verify
    :width="300"
    :height="40"
    text="请按住滑块拖动"
    successText="验证通过"
    @success="handleSuccess"
  />
</template>

<script>
import DragVerify from 'vue-drag-verify'

export default {
  components: { DragVerify },
  methods: {
    handleSuccess() {
      console.log('验证通过')
    }
  }
}
</script>

自定义实现滑动验证

如果需要完全自定义,可以手动实现滑动验证逻辑。

模板部分:

<template>
  <div class="slider-container">
    <div class="slider-track">
      <div 
        class="slider-button"
        @mousedown="startDrag"
        @touchstart="startDrag"
      ></div>
    </div>
    <div class="slider-text">{{ dragText }}</div>
  </div>
</template>

脚本部分:

<script>
export default {
  data() {
    return {
      isDragging: false,
      startX: 0,
      currentX: 0,
      maxWidth: 300,
      dragText: '请滑动验证'
    }
  },
  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)
      document.addEventListener('mouseup', this.endDrag)
      document.addEventListener('touchend', this.endDrag)
    },
    onDrag(e) {
      if (!this.isDragging) return
      const clientX = e.type.includes('touch') ? e.touches[0].clientX : e.clientX
      this.currentX = Math.min(
        this.maxWidth,
        Math.max(0, clientX - this.startX)
      )

      if (this.currentX >= this.maxWidth - 10) {
        this.dragText = '验证通过'
        this.endDrag()
        this.$emit('success')
      }
    },
    endDrag() {
      this.isDragging = false
      document.removeEventListener('mousemove', this.onDrag)
      document.removeEventListener('touchmove', this.onDrag)
      document.removeEventListener('mouseup', this.endDrag)
      document.removeEventListener('touchend', this.endDrag)

      if (this.currentX < this.maxWidth - 10) {
        this.currentX = 0
        this.dragText = '请滑动验证'
      }
    }
  }
}
</script>

样式部分:

<style scoped>
.slider-container {
  width: 300px;
  margin: 20px auto;
}

.slider-track {
  width: 100%;
  height: 40px;
  background: #f5f5f5;
  border-radius: 20px;
  position: relative;
}

.slider-button {
  width: 40px;
  height: 40px;
  background: #409eff;
  border-radius: 50%;
  position: absolute;
  left: 0;
  top: 0;
  cursor: pointer;
}

.slider-text {
  text-align: center;
  margin-top: 10px;
  color: #666;
}
</style>

使用 vue-monoplasty-slide-verify

另一个流行的选择是 vue-monoplasty-slide-verify 组件。

安装:

npm install vue-monoplasty-slide-verify --save

使用示例:

vue实现滑动验证

<template>
  <slide-verify
    :l="42"
    :r="10"
    :w="310"
    :h="155"
    slider-text="向右滑动"
    @success="onSuccess"
    @fail="onFail"
  ></slide-verify>
</template>

<script>
import SlideVerify from 'vue-monoplasty-slide-verify'

export default {
  components: { SlideVerify },
  methods: {
    onSuccess() {
      console.log('验证成功')
    },
    onFail() {
      console.log('验证失败')
    }
  }
}
</script>

注意事项

  • 移动端适配需要考虑 touch 事件
  • 验证成功后应该禁用再次滑动
  • 可以添加加载动画增强用户体验
  • 服务端应该二次验证防止绕过前端验证
  • 滑动轨迹分析可以增加安全性

标签: vue
分享给朋友:

相关文章

eventbus vue实现

eventbus vue实现

EventBus 实现原理 EventBus 是一种发布/订阅模式,用于 Vue 组件间的通信。核心是通过一个中央事件总线管理事件监听和触发。 创建 EventBus 在 Vue 项目中创建一个独立…

vue广告实现

vue广告实现

Vue 广告实现方法 在 Vue 项目中实现广告功能可以通过多种方式完成,具体取决于广告类型和需求。以下是几种常见的实现方法: 使用第三方广告平台 许多广告平台如 Google AdSense、百…

vue实现验证

vue实现验证

Vue 表单验证实现方法 在Vue中实现表单验证可以通过多种方式完成,以下是常见的几种方法: 使用Vuelidate库 Vuelidate是一个轻量级的Vue表单验证库,安装后可以通过简单的配置实现…

lua 实现vue

lua 实现vue

在 Lua 中实现类似 Vue 的响应式框架需要模拟 Vue 的核心功能,包括数据绑定、模板渲染和依赖追踪。以下是实现的关键思路和代码示例: 响应式数据绑定 使用 Lua 的 __index 和 _…

vue for实现

vue for实现

Vue 中的 v-for 指令 v-for 是 Vue.js 中用于渲染列表数据的核心指令,通过遍历数组或对象生成动态内容。 基本语法(数组) <ul> <li v-for…

node vue实现

node vue实现

Node.js 与 Vue.js 实现方案 环境搭建 安装 Node.js(建议 LTS 版本),通过 npm 或 yarn 初始化项目。Vue.js 可通过 Vue CLI 快速搭建: npm…