当前位置:首页 > VUE

vue实现拖拽改变尺寸

2026-01-07 03:30:26VUE

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

使用原生 HTML5 拖拽 API

通过监听 mousedownmousemovemouseup 事件实现拖拽调整尺寸。创建一个可拖拽的元素,计算鼠标移动距离并更新元素尺寸。

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

<script>
export default {
  methods: {
    startResize(e) {
      window.addEventListener('mousemove', this.resize);
      window.addEventListener('mouseup', this.stopResize);
    },
    resize(e) {
      const { width, height } = this.$refs.resizable.getBoundingClientRect();
      this.$refs.resizable.style.width = `${width + e.movementX}px`;
      this.$refs.resizable.style.height = `${height + e.movementY}px`;
    },
    stopResize() {
      window.removeEventListener('mousemove', this.resize);
      window.removeEventListener('mouseup', this.stopResize);
    }
  }
};
</script>

<style>
.resizable {
  position: relative;
  width: 200px;
  height: 200px;
  border: 1px solid #000;
}
.resizer {
  position: absolute;
  right: 0;
  bottom: 0;
  width: 10px;
  height: 10px;
  background: #000;
  cursor: se-resize;
}
</style>

使用第三方库 vue-draggable-resizable

安装 vue-draggable-resizable 库,通过预定义的组件快速实现拖拽和调整尺寸功能。

npm install vue-draggable-resizable
<template>
  <div>
    <VueDraggableResizable :w="200" :h="200" @dragging="onDrag" @resizing="onResize" />
  </div>
</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(x, y, width, height);
    }
  }
};
</script>

使用 interact.js 实现高级拖拽调整

interact.js 是一个功能强大的拖拽库,支持复杂的拖拽和调整尺寸操作。

npm install interactjs
<template>
  <div class="resizable" ref="resizable"></div>
</template>

<script>
import interact from 'interactjs';

export default {
  mounted() {
    interact(this.$refs.resizable)
      .resizable({
        edges: { right: true, bottom: true },
        listeners: {
          move: (event) => {
            const { width, height } = event.rect;
            event.target.style.width = `${width}px`;
            event.target.style.height = `${height}px`;
          }
        }
      });
  }
};
</script>

<style>
.resizable {
  width: 200px;
  height: 200px;
  border: 1px solid #000;
}
</style>

使用 CSS resize 属性

对于简单的调整尺寸需求,可以直接使用 CSS 的 resize 属性,但功能较为有限。

<template>
  <div class="resizable"></div>
</template>

<style>
.resizable {
  width: 200px;
  height: 200px;
  border: 1px solid #000;
  resize: both;
  overflow: auto;
}
</style>

以上方法可以根据具体需求选择,从简单的 CSS 实现到复杂的第三方库方案,灵活应对不同场景。

vue实现拖拽改变尺寸

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

相关文章

vue实现拖拽到容器里

vue实现拖拽到容器里

实现拖拽到容器的基本步骤 使用Vue实现拖拽功能可以借助HTML5的拖放API或第三方库如vuedraggable。以下是两种常见方法的详细说明。 使用HTML5拖放API HTML5原生支持拖放…

vue 实现拖拽

vue 实现拖拽

Vue 实现拖拽功能 使用 HTML5 原生拖拽 API HTML5 提供了原生的拖拽 API,可以通过 draggable 属性和相关事件实现拖拽功能。 <template> &…

vue拖拽实现

vue拖拽实现

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

vue拖拽实现方式

vue拖拽实现方式

Vue拖拽实现方式 使用HTML5原生拖拽API HTML5提供了原生的拖拽API,可以通过draggable属性、dragstart、dragend、dragover和drop等事件实现基础拖拽功能…

vue实现拖拽div

vue实现拖拽div

Vue 实现拖拽 Div 的方法 使用原生 HTML5 拖拽 API HTML5 提供了原生的拖拽 API,结合 Vue 的事件绑定可以快速实现拖拽功能。以下是实现步骤: <template&…

vue拖拽容器实现

vue拖拽容器实现

实现 Vue 拖拽容器的基本方法 使用 HTML5 原生拖拽 API 通过 draggable 属性和拖拽事件(dragstart、dragend、dragover、drop)实现基础拖拽功能。适用于…