当前位置:首页 > VUE

vue 实现div拖拽

2026-01-17 18:58:09VUE

实现 div 拖拽的基本思路

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

使用原生事件实现拖拽

  1. 模板部分
    在模板中绑定事件,并通过 style 动态设置 div 的位置:

    <template>
      <div
        class="draggable"
        @mousedown="startDrag"
        @mousemove="onDrag"
        @mouseup="stopDrag"
        @mouseleave="stopDrag"
        :style="{ left: position.x + 'px', top: position.y + 'px' }"
      >
        可拖拽的 div
      </div>
    </template>
  2. 脚本部分
    定义拖拽逻辑,记录初始位置和偏移量:

    <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,
          };
        },
        onDrag(e) {
          if (!this.isDragging) return;
          this.position = {
            x: e.clientX - this.startPos.x,
            y: e.clientY - this.startPos.y,
          };
        },
        stopDrag() {
          this.isDragging = false;
        },
      },
    };
    </script>
  3. 样式部分
    确保 div 可以自由移动:

    <style>
    .draggable {
      position: absolute;
      cursor: grab;
      user-select: none;
    }
    </style>

使用第三方库(如 vuedraggable)

如果需要更复杂的拖拽功能(如列表排序),可以使用 vuedraggable 库:

  1. 安装库

    npm install vuedraggable
  2. 实现列表拖拽

    vue 实现div拖拽

    <template>
      <draggable v-model="list" @start="onStart" @end="onEnd">
        <div v-for="item in list" :key="item.id">
          {{ item.name }}
        </div>
      </draggable>
    </template>
    
    <script>
    import draggable from "vuedraggable";
    export default {
      components: { draggable },
      data() {
        return {
          list: [
            { id: 1, name: "Item 1" },
            { id: 2, name: "Item 2" },
          ],
        };
      },
      methods: {
        onStart() {
          console.log("拖拽开始");
        },
        onEnd() {
          console.log("拖拽结束");
        },
      },
    };
    </script>

注意事项

  • 拖拽性能优化:避免频繁触发 mousemove 事件,可以使用防抖(debounce)或节流(throttle)。
  • 边界检查:防止 div 拖拽超出可视区域。
  • 移动端适配:如果需要支持触摸屏,需监听 touchstarttouchmovetouchend 事件。

通过以上方法,可以灵活实现 Vue 中的 div 拖拽功能。

标签: 拖拽vue
分享给朋友:

相关文章

vue实现拖拽计算器

vue实现拖拽计算器

Vue 拖拽计算器实现方案 核心功能分析 可拖拽组件:计算器整体或按钮需要支持拖拽 2.计算逻辑:实现基本四则运算功能 布局系统:支持动态调整计算器位置 基础实现步骤 安装依赖 需要vue-dragg…

vue实现tablegrid

vue实现tablegrid

Vue 实现 TableGrid 的方法 使用 Element UI 的 Table 组件 Element UI 提供了强大的 Table 组件,可以快速实现表格布局。安装 Element UI 后,…

vue实现管道

vue实现管道

Vue 中实现管道(Pipe)功能 在 Vue 中可以通过过滤器(Filters)或计算属性(Computed Properties)实现类似管道的功能,将数据经过多个处理步骤后输出。 使用过滤器(…

vue实现建模

vue实现建模

Vue 实现建模的方法 在 Vue 中实现建模通常涉及数据绑定、组件化和状态管理。以下是几种常见的方法: 数据驱动建模 Vue 的核心是数据驱动视图。通过定义数据模型,Vue 会自动更新 DOM。例…

vue实现api

vue实现api

Vue 实现 API 调用 在 Vue 中调用 API 通常涉及使用 axios 或 fetch 发送 HTTP 请求,并结合 Vue 的响应式特性管理数据。以下是常见的实现方式: 安装 axios…

vue实现刷卡

vue实现刷卡

Vue 实现刷卡效果 在 Vue 中实现刷卡效果可以通过 CSS 动画和 Vue 的过渡系统结合完成。以下是一种常见的实现方式: 基础实现方法 安装 Vue 过渡依赖(如果尚未安装): npm i…