当前位置:首页 > VUE

vue实现拼图验证

2026-02-17 03:50:06VUE

vue实现拼图验证的方法

使用第三方库vue-puzzle-verification

安装vue-puzzle-verification库:

npm install vue-puzzle-verification --save

在Vue组件中引入并使用:

import VuePuzzleVerification from 'vue-puzzle-verification'

export default {
  components: {
    VuePuzzleVerification
  },
  methods: {
    handleSuccess() {
      console.log('验证成功')
    }
  }
}

模板中使用:

vue实现拼图验证

<vue-puzzle-verification @success="handleSuccess"></vue-puzzle-verification>

自定义实现拼图验证

创建Vue组件,包含以下功能:

<template>
  <div class="puzzle-container">
    <div class="puzzle-bg" :style="{backgroundImage: `url(${bgImage})`}"></div>
    <div 
      class="puzzle-piece" 
      :style="{left: `${pieceLeft}px`, backgroundImage: `url(${bgImage})`, backgroundPosition: `-${pieceLeft}px 0`}"
      @mousedown="startDrag"
    ></div>
    <div class="puzzle-slider">
      <div class="slider-track"></div>
      <div 
        class="slider-thumb" 
        :style="{left: `${sliderLeft}px`}"
        @mousedown="startSlide"
      ></div>
    </div>
  </div>
</template>

JavaScript逻辑部分:

vue实现拼图验证

export default {
  data() {
    return {
      bgImage: require('./assets/puzzle-bg.jpg'),
      pieceLeft: 0,
      sliderLeft: 0,
      isDragging: false,
      startX: 0
    }
  },
  methods: {
    startDrag(e) {
      this.isDragging = true
      this.startX = e.clientX
      document.addEventListener('mousemove', this.dragPiece)
      document.addEventListener('mouseup', this.stopDrag)
    },
    dragPiece(e) {
      if (!this.isDragging) return
      const moveX = e.clientX - this.startX
      this.pieceLeft = Math.max(0, Math.min(200, moveX))
      this.sliderLeft = this.pieceLeft
    },
    stopDrag() {
      this.isDragging = false
      document.removeEventListener('mousemove', this.dragPiece)
      document.removeEventListener('mouseup', this.stopDrag)
      this.verifyPosition()
    },
    verifyPosition() {
      const threshold = 5
      const targetPosition = 150
      if (Math.abs(this.pieceLeft - targetPosition) < threshold) {
        this.$emit('success')
      } else {
        this.resetPuzzle()
      }
    },
    resetPuzzle() {
      this.pieceLeft = 0
      this.sliderLeft = 0
    }
  }
}

CSS样式部分:

.puzzle-container {
  position: relative;
  width: 300px;
  height: 200px;
}

.puzzle-bg {
  width: 100%;
  height: 100%;
  background-size: cover;
}

.puzzle-piece {
  position: absolute;
  top: 50px;
  width: 50px;
  height: 50px;
  cursor: move;
  background-size: 300px 200px;
}

.puzzle-slider {
  position: relative;
  width: 100%;
  height: 40px;
  margin-top: 10px;
  background: #f5f5f5;
}

.slider-track {
  height: 2px;
  background: #ccc;
  margin-top: 19px;
}

.slider-thumb {
  position: absolute;
  width: 20px;
  height: 20px;
  top: 10px;
  background: #409eff;
  border-radius: 50%;
  cursor: pointer;
}

移动端适配

添加触摸事件支持:

methods: {
  startDrag(e) {
    const clientX = e.type.includes('touch') ? e.touches[0].clientX : e.clientX
    this.isDragging = true
    this.startX = clientX
    document.addEventListener('mousemove', this.dragPiece)
    document.addEventListener('touchmove', this.dragPiece)
    document.addEventListener('mouseup', this.stopDrag)
    document.addEventListener('touchend', this.stopDrag)
  },
  dragPiece(e) {
    if (!this.isDragging) return
    const clientX = e.type.includes('touch') ? e.touches[0].clientX : e.clientX
    const moveX = clientX - this.startX
    this.pieceLeft = Math.max(0, Math.min(200, moveX))
    this.sliderLeft = this.pieceLeft
  },
  stopDrag() {
    this.isDragging = false
    document.removeEventListener('mousemove', this.dragPiece)
    document.removeEventListener('touchmove', this.dragPiece)
    document.removeEventListener('mouseup', this.stopDrag)
    document.removeEventListener('touchend', this.stopDrag)
    this.verifyPosition()
  }
}

模板中添加触摸事件:

<div 
  class="puzzle-piece"
  @mousedown="startDrag"
  @touchstart="startDrag"
></div>
<div 
  class="slider-thumb"
  @mousedown="startSlide"
  @touchstart="startSlide"
></div>

标签: 拼图vue
分享给朋友:

相关文章

vue实现多级弹窗

vue实现多级弹窗

Vue 多级弹窗实现方法 方法一:使用动态组件与递归组件 在 Vue 中可以通过动态组件配合递归组件实现多级弹窗。定义一个基础弹窗组件,通过递归调用自身实现层级嵌套。 基础弹窗组件模板示例: &…

vue 实现拖拽

vue 实现拖拽

Vue 实现拖拽功能 使用 HTML5 原生拖拽 API HTML5 提供了原生的拖拽 API,可以通过 draggable 属性和相关事件实现拖拽功能。 <template> &l…

分页实现vue

分页实现vue

分页实现(Vue) 在Vue中实现分页功能通常需要结合前端分页逻辑和后端API支持。以下是两种常见的实现方式: 前端分页实现 适用于数据量较小的情况,直接在客户端完成分页逻辑。 <templ…

vue实现123456

vue实现123456

Vue 实现数字 123456 的显示 在 Vue 中显示数字 123456 非常简单,可以通过以下方法实现: 方法一:直接在模板中显示 <template> <div>…

vue实现github

vue实现github

Vue 实现 GitHub 相关功能 创建 GitHub API 请求 在 Vue 项目中,可以使用 axios 或 fetch 与 GitHub API 进行交互。需要在 GitHub 上创建个人访…

vue实现tabbar

vue实现tabbar

Vue 实现 TabBar 的方法 使用 Vue Router 实现基础 TabBar 安装 Vue Router 依赖: npm install vue-router 创建路由配置文件(route…