当前位置:首页 > 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. 脚本部分
    定义拖拽逻辑,记录初始位置和偏移量:

    vue 实现div拖拽

    <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 库:

vue 实现div拖拽

  1. 安装库

    npm install vuedraggable
  2. 实现列表拖拽

    <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中实现收起展开面板功能可以通过多种方式完成,以下是几种常见的实现方法: 使用v-show或v-if控制显示隐藏 利用Vue的指令可以轻松实现面板的展开和收起:…

vue实现点击

vue实现点击

实现点击事件的基本方法 在Vue中实现点击事件可以通过v-on指令或@简写方式绑定。以下是常见的点击事件绑定方法: <template> <button v-on:click…

vue实现granfana

vue实现granfana

Vue 实现类似 Grafana 的仪表盘 要在 Vue 中实现类似 Grafana 的仪表盘功能,需要结合数据可视化库、状态管理和 UI 组件。以下是关键步骤和推荐工具: 数据可视化库 使用 E…

vue 全景实现

vue 全景实现

Vue 全景实现方案 在Vue中实现全景效果,通常需要结合WebGL或第三方库来处理3D场景渲染。以下是几种常见方法: 使用Three.js库 Three.js是一个强大的WebGL库,适合创建3D…

vue  实现tab

vue 实现tab

实现 Tab 切换功能 在 Vue 中实现 Tab 切换功能可以通过动态组件或条件渲染完成。以下是两种常见方法: 使用 v-if 或 v-show 实现条件渲染 通过绑定 currentTab 变量…

vue 组件实现

vue 组件实现

Vue 组件实现方法 单文件组件 (SFC) 使用 .vue 文件格式,包含模板、脚本和样式三部分: <template> <div class="example">{{…