当前位置:首页 > 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-audio-recorder 或原生 Web Audio API。录音文件播放通常涉及录音、存储和播放三个核心环节。…

vue实现走势图

vue实现走势图

Vue 实现走势图的方法 使用 ECharts 库 ECharts 是一个强大的数据可视化库,支持多种图表类型,包括走势图(折线图)。在 Vue 项目中可以通过 vue-echarts 封装库或直接使…

vue实现frame

vue实现frame

Vue 中实现 iframe 的方法 在 Vue 中可以通过直接使用 <iframe> 标签或动态绑定 src 属性来实现 iframe 功能。 基本用法 <template&g…

vue实现list

vue实现list

Vue 实现列表渲染 在 Vue 中,可以使用 v-for 指令来渲染列表。v-for 指令基于一个数组或对象来渲染一个列表,可以遍历数组或对象的属性。 基本列表渲染 <template>…

vue实现 treegrid

vue实现 treegrid

Vue 实现 TreeGrid 的方法 使用现成组件库 Element UI、Ant Design Vue 等流行组件库提供了现成的 TreeTable/TreeGrid 组件,可直接使用。 Ele…

vue前后端实现

vue前后端实现

Vue 前后端实现方案 Vue 作为前端框架,通常需要与后端服务结合使用。以下是常见的实现方式: 前端实现(Vue) 使用 Vue CLI 或 Vite 创建项目: npm init vue@la…