当前位置:首页 > VUE

vue实现拖拽改变尺寸

2026-02-25 21:20:45VUE

Vue 实现拖拽改变尺寸的方法

使用 Vue 实现拖拽改变尺寸可以通过多种方式完成,以下是几种常见的方法:

使用原生拖拽事件

通过监听 mousedownmousemovemouseup 事件实现拖拽功能。这种方式灵活性强,适合自定义需求。

<template>
  <div class="resizable-box" ref="resizableBox">
    <div class="resizer" @mousedown="startResize"></div>
  </div>
</template>

<script>
export default {
  methods: {
    startResize(e) {
      const box = this.$refs.resizableBox;
      const startX = e.clientX;
      const startWidth = box.offsetWidth;

      const doResize = (e) => {
        const newWidth = startWidth + (e.clientX - startX);
        box.style.width = `${newWidth}px`;
      };

      const stopResize = () => {
        document.removeEventListener('mousemove', doResize);
        document.removeEventListener('mouseup', stopResize);
      };

      document.addEventListener('mousemove', doResize);
      document.addEventListener('mouseup', stopResize);
    }
  }
};
</script>

<style>
.resizable-box {
  position: relative;
  width: 200px;
  height: 200px;
  border: 1px solid #ccc;
}

.resizer {
  position: absolute;
  right: 0;
  bottom: 0;
  width: 10px;
  height: 10px;
  background: #333;
  cursor: se-resize;
}
</style>

使用第三方库

Vue Draggable Resizable 是一个专门为 Vue 设计的拖拽和调整尺寸的库,功能强大且易于使用。

安装库:

npm install vue-draggable-resizable

使用示例:

<template>
  <VueDraggableResizable
    :w="200"
    :h="200"
    @dragging="onDrag"
    @resizing="onResize"
  >
    <p>可拖拽和调整大小的元素</p>
  </VueDraggableResizable>
</template>

<script>
import VueDraggableResizable from 'vue-draggable-resizable';

export default {
  components: { VueDraggableResizable },
  methods: {
    onDrag(x, y) {
      console.log(`位置: ${x}, ${y}`);
    },
    onResize(x, y, width, height) {
      console.log(`尺寸: ${width}x${height}`);
    }
  }
};
</script>

结合 Vue 指令

自定义 Vue 指令可以简化拖拽功能的实现,适用于需要复用的场景。

vue实现拖拽改变尺寸

<template>
  <div v-resizable class="resizable-box"></div>
</template>

<script>
export default {
  directives: {
    resizable: {
      bind(el) {
        const resizer = document.createElement('div');
        resizer.className = 'resizer';
        el.appendChild(resizer);

        resizer.addEventListener('mousedown', initResize);

        function initResize(e) {
          const startX = e.clientX;
          const startWidth = el.offsetWidth;

          function doResize(e) {
            el.style.width = `${startWidth + (e.clientX - startX)}px`;
          }

          function stopResize() {
            document.removeEventListener('mousemove', doResize);
            document.removeEventListener('mouseup', stopResize);
          }

          document.addEventListener('mousemove', doResize);
          document.addEventListener('mouseup', stopResize);
        }
      }
    }
  }
};
</script>

注意事项

  • 拖拽过程中注意性能优化,避免频繁的 DOM 操作。
  • 对于复杂的拖拽逻辑,建议使用成熟的第三方库以减少开发时间。
  • 移动端适配可能需要额外处理触摸事件(touchstarttouchmovetouchend)。

标签: 拖拽尺寸
分享给朋友:

相关文章

vue 实现拖拽功能

vue 实现拖拽功能

Vue 实现拖拽功能的方法 使用 HTML5 原生拖拽 API HTML5 提供了原生的拖拽 API,可以通过 draggable 属性、dragstart、dragend、dragover 和 dr…

vue实现鼠标拖拽

vue实现鼠标拖拽

实现鼠标拖拽功能 在Vue中实现鼠标拖拽功能可以通过以下步骤完成。这里提供一个基础实现方案,适用于大多数拖拽场景。 监听鼠标事件 为需要拖拽的元素绑定mousedown、mousemove和mous…

vue实现拖拽互换

vue实现拖拽互换

Vue 实现拖拽互换 在 Vue 中实现拖拽互换功能可以通过原生 HTML5 的拖放 API 或第三方库如 vuedraggable 来完成。以下是两种方法的实现方式: 使用 HTML5 拖放 AP…

vue实现横向拖拽

vue实现横向拖拽

实现横向拖拽的基本思路 使用 Vue 实现横向拖拽功能通常涉及监听鼠标事件(mousedown、mousemove、mouseup),计算拖拽距离,并动态更新元素位置。以下是核心实现方法: 基础实现…

vue 实现 拖拽组件

vue 实现 拖拽组件

Vue 实现拖拽组件的方法 使用 HTML5 原生拖拽 API HTML5 提供了原生的拖拽 API,可以通过 draggable 属性、dragstart、dragend、dragover 和 dr…

vue实现菜单拖拽

vue实现菜单拖拽

Vue 实现菜单拖拽 实现菜单拖拽功能可以通过 Vue 的指令或第三方库(如 vuedraggable)来完成。以下是两种常见的方法: 使用 vuedraggable 库 vuedraggable…