当前位置:首页 > VUE

vue实现移动按钮

2026-03-30 06:52:21VUE

Vue 实现移动按钮的方法

使用拖拽事件实现

在Vue中可以通过@mousedown@mousemove@mouseup事件实现按钮的拖拽移动效果。需要记录鼠标按下时的初始位置和元素当前的位置。

<template>
  <div 
    class="draggable-btn"
    :style="{ left: position.x + 'px', top: position.y + 'px' }"
    @mousedown="startDrag"
  >
    拖拽我
  </div>
</template>

<script>
export default {
  data() {
    return {
      position: { x: 0, y: 0 },
      isDragging: false,
      startPos: { x: 0, y: 0 }
    }
  },
  methods: {
    startDrag(e) {
      this.isDragging = true
      this.startPos = {
        x: e.clientX - this.position.x,
        y: e.clientY - this.position.y
      }
      document.addEventListener('mousemove', this.onDrag)
      document.addEventListener('mouseup', this.stopDrag)
    },
    onDrag(e) {
      if (!this.isDragging) return
      this.position = {
        x: e.clientX - this.startPos.x,
        y: e.clientY - this.startPos.y
      }
    },
    stopDrag() {
      this.isDragging = false
      document.removeEventListener('mousemove', this.onDrag)
      document.removeEventListener('mouseup', this.stopDrag)
    }
  }
}
</script>

<style>
.draggable-btn {
  position: absolute;
  width: 100px;
  height: 40px;
  background-color: #42b983;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: move;
  user-select: none;
}
</style>

使用第三方库实现

对于更复杂的拖拽需求,可以使用专门的拖拽库如vuedraggablesortablejs

安装vuedraggable:

npm install vuedraggable

使用示例:

<template>
  <draggable v-model="list" @start="drag=true" @end="drag=false">
    <div v-for="element in list" :key="element.id">
      {{ element.name }}
    </div>
  </draggable>
</template>

<script>
import draggable from 'vuedraggable'

export default {
  components: { draggable },
  data() {
    return {
      list: [
        { name: "Item 1", id: 1 },
        { name: "Item 2", id: 2 }
      ]
    }
  }
}
</script>

移动端触摸事件支持

针对移动设备,需要使用触摸事件替代鼠标事件:

<template>
  <div 
    class="draggable-btn"
    :style="{ left: position.x + 'px', top: position.y + 'px' }"
    @touchstart="startDrag"
    @touchmove="onDrag"
    @touchend="stopDrag"
  >
    拖拽我
  </div>
</template>

<script>
export default {
  data() {
    return {
      position: { x: 0, y: 0 },
      isDragging: false,
      startPos: { x: 0, y: 0 }
    }
  },
  methods: {
    startDrag(e) {
      this.isDragging = true
      const touch = e.touches[0]
      this.startPos = {
        x: touch.clientX - this.position.x,
        y: touch.clientY - this.position.y
      }
    },
    onDrag(e) {
      if (!this.isDragging) return
      const touch = e.touches[0]
      this.position = {
        x: touch.clientX - this.startPos.x,
        y: touch.clientY - this.startPos.y
      }
      e.preventDefault()
    },
    stopDrag() {
      this.isDragging = false
    }
  }
}
</script>

边界限制

可以为可移动元素添加边界限制,防止移出可视区域:

vue实现移动按钮

onDrag(e) {
  if (!this.isDragging) return
  let newX = e.clientX - this.startPos.x
  let newY = e.clientY - this.startPos.y

  // 限制在窗口范围内
  const maxX = window.innerWidth - this.$el.offsetWidth
  const maxY = window.innerHeight - this.$el.offsetHeight

  newX = Math.max(0, Math.min(newX, maxX))
  newY = Math.max(0, Math.min(newY, maxY))

  this.position = { x: newX, y: newY }
}

标签: 按钮vue
分享给朋友:

相关文章

vue实现积分

vue实现积分

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

vue评分实现

vue评分实现

Vue 评分组件实现方法 使用第三方库(如 Element UI) 安装 Element UI: npm install element-ui 引入并注册组件: import Vue from '…

vue实现级联

vue实现级联

Vue 实现级联选择器的方法 级联选择器(Cascader)是常见的表单组件,用于处理多层级联动数据选择(如省市区选择)。以下是几种实现方式: 基于 Element UI 的 el-cascader…

vue实现RTMP

vue实现RTMP

Vue 中实现 RTMP 流播放 RTMP(Real-Time Messaging Protocol)是一种用于实时音视频流传输的协议。在 Vue 中实现 RTMP 播放通常需要借助第三方库或播放器。…

vue实现popup

vue实现popup

Vue 实现 Popup 弹窗 使用 Vue 原生组件 创建一个基本的 Vue 组件作为弹窗,通过 v-if 或 v-show 控制显示隐藏。 <template> <div&…

vue实现换肤

vue实现换肤

使用 CSS 变量实现换肤 在 Vue 项目中,可以通过 CSS 变量(Custom Properties)动态切换主题色。CSS 变量可以在运行时修改,适合实现换肤功能。 定义全局 CSS 变量:…