当前位置:首页 > VUE

vue实现拖动宽度

2026-01-18 12:09:50VUE

Vue 实现拖动调整宽度

在 Vue 中实现拖动调整宽度可以通过监听鼠标事件和使用 CSS 样式来实现。以下是具体实现方法:

使用鼠标事件监听

创建一个可拖动的分隔条,通过监听 mousedownmousemovemouseup 事件来调整宽度。

<template>
  <div class="container">
    <div class="left-panel" :style="{ width: leftWidth + 'px' }">左侧面板</div>
    <div class="divider" @mousedown="startDrag"></div>
    <div class="right-panel">右侧面板</div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      leftWidth: 200,
      isDragging: false
    };
  },
  methods: {
    startDrag(e) {
      this.isDragging = true;
      document.addEventListener('mousemove', this.onDrag);
      document.addEventListener('mouseup', this.stopDrag);
    },
    onDrag(e) {
      if (this.isDragging) {
        this.leftWidth = e.clientX;
      }
    },
    stopDrag() {
      this.isDragging = false;
      document.removeEventListener('mousemove', this.onDrag);
      document.removeEventListener('mouseup', this.stopDrag);
    }
  }
};
</script>

<style>
.container {
  display: flex;
  height: 100vh;
}
.left-panel {
  background: #f0f0f0;
}
.divider {
  width: 5px;
  background: #ccc;
  cursor: col-resize;
}
.right-panel {
  flex: 1;
  background: #e0e0e0;
}
</style>

使用第三方库

如果需要更复杂的功能,可以使用第三方库如 vue-resizableinteract.js

vue实现拖动宽度

使用 interact.js 实现

安装 interact.js

npm install interactjs

在 Vue 组件中使用:

vue实现拖动宽度

<template>
  <div class="container">
    <div ref="resizable" class="resizable">可拖动调整宽度的元素</div>
  </div>
</template>

<script>
import interact from 'interactjs';

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

<style>
.resizable {
  width: 200px;
  height: 100px;
  background: #f0f0f0;
  border: 1px solid #ccc;
}
</style>

使用 Vue 指令封装

可以封装一个自定义指令来简化拖动逻辑。

Vue.directive('resize', {
  bind(el, binding) {
    const direction = binding.value || 'horizontal';
    const handler = document.createElement('div');
    handler.className = 'resize-handler';

    el.appendChild(handler);
    el.style.position = 'relative';

    handler.addEventListener('mousedown', (e) => {
      document.addEventListener('mousemove', resize);
      document.addEventListener('mouseup', stopResize);
    });

    function resize(e) {
      if (direction === 'horizontal') {
        el.style.width = `${e.clientX - el.getBoundingClientRect().left}px`;
      }
    }

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

在模板中使用:

<div v-resize="'horizontal'" style="width: 200px; height: 100px; background: #f0f0f0;"></div>

注意事项

  • 拖动过程中可能需要限制最小和最大宽度,可以在事件处理函数中添加逻辑。
  • 在移动端需要额外处理触摸事件(touchstarttouchmovetouchend)。
  • 使用第三方库时,注意在组件销毁时清理事件监听,避免内存泄漏。

标签: 拖动宽度
分享给朋友:

相关文章

vue实现拖动文件移动

vue实现拖动文件移动

Vue 实现文件拖动移动 实现文件拖动移动功能通常涉及 HTML5 的拖放 API 和 Vue 的事件处理。以下是具体实现方法: 基础拖放实现 在 Vue 组件中设置拖放区域并处理相关事件: &l…

vue.js实现拖动

vue.js实现拖动

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

React如何让modal可以被拖动

React如何让modal可以被拖动

实现可拖动Modal的方法 在React中实现可拖动的Modal,可以通过结合HTML5的拖拽API或第三方库如react-draggable来完成。以下是两种常用方法的详细说明: 使用react-…

react如何获取遍历元素的宽度

react如何获取遍历元素的宽度

在React中获取遍历元素的宽度可以通过多种方法实现,以下是几种常见的方式: 使用useRef和useEffect钩子 通过useRef创建引用并绑定到目标元素上,在useEffect中获取元素的宽…

js实现调整宽度

js实现调整宽度

调整元素宽度的 JavaScript 方法 直接修改 style.width 属性 通过 JavaScript 直接修改元素的 style.width 属性可以调整宽度。这种方式会直接作用于元素的内联…

js实现拖动元素

js实现拖动元素

实现元素拖动的基本原理 通过监听鼠标事件(mousedown、mousemove、mouseup)实现元素拖动。核心逻辑是计算鼠标移动距离并更新元素位置。 基本实现代码 const draggabl…