当前位置:首页 > 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-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逻辑部分:

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的响应式数据和事件绑定特性来完成。主要思路是利用v-show或v-if控制下拉菜单的显示与隐藏,并通过鼠标事件或点击事件触发状态变化。…

vue实现积分

vue实现积分

Vue 实现积分功能 在 Vue 中实现积分功能通常涉及前端展示、交互逻辑以及与后端 API 的通信。以下是常见的实现方法和步骤: 积分展示组件 创建一个积分展示组件,用于显示当前用户的积分。可以…

vue实现jqueryui

vue实现jqueryui

Vue 实现 jQuery UI 功能 在 Vue 项目中实现类似 jQuery UI 的功能,可以通过原生 Vue 组件或第三方库来实现。以下是几种常见 jQuery UI 功能的 Vue 替代方案…

vue实现排序

vue实现排序

Vue 实现排序的方法 在 Vue 中实现排序可以通过多种方式完成,以下是一些常见的方法: 使用计算属性排序数组 计算属性非常适合对数据进行排序,因为它会在依赖的数据变化时自动更新。以下是一个示例…

vue 实现列表

vue 实现列表

Vue 实现列表的方法 在 Vue 中实现列表通常使用 v-for 指令,结合数组或对象数据渲染动态列表。以下是几种常见的实现方式: 基础列表渲染 使用 v-for 指令遍历数组,渲染列表项。it…

vue 实现拖动

vue 实现拖动

Vue 实现拖动的几种方法 在Vue中实现拖动功能可以通过多种方式,包括原生HTML5的拖放API、第三方库如vuedraggable等。以下是几种常见的实现方法: 使用HTML5拖放API HT…