当前位置:首页 > VUE

vue实现盒子拖拽

2026-01-16 19:50:38VUE

实现盒子拖拽的基本思路

在Vue中实现盒子拖拽功能,可以通过监听鼠标事件(mousedownmousemovemouseup)来实现。核心逻辑是记录拖拽起始位置,计算位移差值,并更新盒子的位置。

基础实现代码示例

<template>
  <div 
    class="draggable-box" 
    ref="box"
    :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-box {
  position: absolute;
  width: 100px;
  height: 100px;
  background-color: #42b983;
  cursor: move;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
}
</style>

优化拖拽性能

为避免频繁触发mousemove事件导致性能问题,可以使用requestAnimationFrame优化:

vue实现盒子拖拽

onDrag(e) {
  if (!this.isDragging) return;
  requestAnimationFrame(() => {
    this.position = {
      x: e.clientX - this.startPos.x,
      y: e.clientY - this.startPos.y
    };
  });
}

支持触摸设备

为兼容移动端触摸事件,需添加touchstarttouchmovetouchend事件:

vue实现盒子拖拽

<div
  @mousedown="startDrag"
  @touchstart="startDrag"
></div>
startDrag(e) {
  const clientX = e.clientX || e.touches[0].clientX;
  const clientY = e.clientY || e.touches[0].clientY;
  // 其余逻辑不变
}

使用第三方库

若需更复杂的功能(如拖拽排序、边界限制),推荐使用以下库:

  • vuedraggable:基于Sortable.js的Vue拖拽组件
  • interact.js:轻量级拖拽库,支持手势和惯性

边界限制示例

限制拖拽范围在父容器内:

onDrag(e) {
  if (!this.isDragging) return;
  const parentRect = this.$refs.box.parentElement.getBoundingClientRect();
  const boxWidth = this.$refs.box.offsetWidth;
  const boxHeight = this.$refs.box.offsetHeight;

  let x = e.clientX - this.startPos.x;
  let y = e.clientY - this.startPos.y;

  x = Math.max(0, Math.min(x, parentRect.width - boxWidth));
  y = Math.max(0, Math.min(y, parentRect.height - boxHeight));

  this.position = { x, y };
}

标签: 盒子拖拽
分享给朋友:

相关文章

vue实现图标拖拽

vue实现图标拖拽

Vue 实现图标拖拽 使用 HTML5 拖放 API 在 Vue 中实现图标拖拽可以利用 HTML5 的拖放 API。通过 draggable 属性标记可拖拽元素,并监听 dragstart、drag…

vue 实现卡片拖拽

vue 实现卡片拖拽

Vue 实现卡片拖拽 使用 HTML5 原生拖拽 API HTML5 提供了原生的拖拽 API,可以通过 draggable 属性、dragstart、dragend、dragover 和 drop…

css盒子模型制作心得

css盒子模型制作心得

理解盒子模型基础概念 CSS盒子模型由内容(content)、内边距(padding)、边框(border)和外边距(margin)组成。每个元素本质上是一个矩形盒子,通过调整这些属性控制布局和间距。…

vue实现拖拽实现布局

vue实现拖拽实现布局

Vue 实现拖拽布局的方法 使用 Vue.Draggable 库 Vue.Draggable 是基于 Sortable.js 的 Vue 组件,适合实现列表或网格的拖拽排序。 安装依赖: npm…

vue实现拖拽连接

vue实现拖拽连接

Vue 实现拖拽连接功能 在 Vue 中实现拖拽连接功能通常涉及使用 HTML5 的拖放 API 或第三方库(如 vuedraggable)。以下是两种常见实现方式: 使用 HTML5 拖放 API…

vue实现盒子平移

vue实现盒子平移

Vue 实现盒子平移的方法 使用 CSS Transition 和 v-bind:style 通过 Vue 的 v-bind:style 动态绑定 CSS 样式,结合 transition 实现平滑的…