当前位置:首页 > VUE

vue实现拖拽改变尺寸

2026-01-07 03:30:26VUE

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

使用原生 HTML5 拖拽 API

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

vue实现拖拽改变尺寸

<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 库,通过预定义的组件快速实现拖拽和调整尺寸功能。

vue实现拖拽改变尺寸

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.Draggable 库 Vue.Draggable 是基于 Sortable.js 的 Vue 组件,适合实现列表或网格的拖拽排序。 安装依赖: np…

vue实现carousel拖拽

vue实现carousel拖拽

实现拖拽轮播的基本思路 在Vue中实现可拖拽的Carousel组件需要结合触摸事件(touchstart、touchmove、touchend)和鼠标事件(mousedown、mousemove、mo…

vue实现面板拖拽

vue实现面板拖拽

Vue 实现面板拖拽的方法 使用 HTML5 原生拖拽 API HTML5 提供了原生的拖拽 API,可以通过 draggable 属性和相关事件实现拖拽功能。 在 Vue 模板中为元素添加 dr…

利用vue实现拖拽

利用vue实现拖拽

使用 Vue 实现拖拽功能 原生 HTML5 拖拽 API Vue 可以结合 HTML5 的拖拽 API 实现基础拖拽功能。以下是一个简单示例: <template> <di…

vue实现盒子拖拽

vue实现盒子拖拽

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

vue实现简单拖拽

vue实现简单拖拽

Vue 实现简单拖拽功能 在 Vue 中实现拖拽功能可以通过 HTML5 的拖放 API 或第三方库(如 vuedraggable)来完成。以下是两种常见的实现方式: 使用 HTML5 拖放 API…