当前位置:首页 > 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 HTML5 提供了原生拖放 API,可以通过 draggable 属性、dragstart、dragend、dragover 和 dro…

jquery 拖动

jquery 拖动

jQuery 拖动实现方法 jQuery本身不直接提供拖动功能,但可以通过结合jQuery UI或原生HTML5的拖放API实现。以下是两种常见方法: 使用jQuery UI实现拖动 jQuery…

vue实现卡片拖动

vue实现卡片拖动

Vue 实现卡片拖动的几种方法 使用 HTML5 拖放 API HTML5 提供了原生的拖放 API,可以在 Vue 中直接使用。通过 draggable 属性标记可拖动的元素,并监听 dragsta…

vue怎么实现拖动

vue怎么实现拖动

Vue 实现拖动的常用方法 使用 HTML5 原生拖放 API HTML5 提供了原生的拖放 API,可以通过 draggable 属性和相关事件实现基础拖动功能。适用于简单场景。 <tem…

vue 实现表格列拖动

vue 实现表格列拖动

实现表格列拖动的核心方法 使用 Vue 实现表格列拖动功能主要依赖 HTML5 的拖放 API 和 Vue 的数据绑定特性。以下是具体实现步骤: 基础拖放 API 设置 为表格列头添加 dragga…

vue实现鼠标拖动多选

vue实现鼠标拖动多选

Vue 实现鼠标拖动多选框 基本思路 通过监听鼠标事件(mousedown、mousemove、mouseup)实现拖动选区,结合动态计算选中元素的范围完成多选功能。核心是记录起始位置和当前鼠标位置,…