当前位置:首页 > 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

使用 interact.js 实现

安装 interact.js

npm install interactjs

在 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 实现卡片拖动

Vue 实现卡片拖动的几种方法 使用 Vue 实现卡片拖动可以通过多种方式实现,以下是几种常见的方法: 使用 HTML5 原生拖放 API HTML5 提供了原生的拖放 API,可以通过 d…

jquery 宽度

jquery 宽度

jQuery 获取和设置元素宽度的方法 获取元素宽度 使用 .width() 方法可以获取匹配元素集合中第一个元素的宽度(不包含内边距、边框和外边距)。该方法返回一个数值,单位为像素。 var wi…

vue实现数据拖动

vue实现数据拖动

Vue 实现数据拖动 使用 HTML5 拖放 API HTML5 提供了原生的拖放 API,可以通过 draggable 属性、dragstart、dragend、dragover 和 drop 事件…

vue实现内容拖动

vue实现内容拖动

Vue 实现内容拖动的方法 使用原生 HTML5 拖放 API 在 Vue 中可以利用 HTML5 的拖放 API 实现基本的拖动功能。通过 draggable 属性、dragstart、dragen…

js实现图片的拖动

js实现图片的拖动

实现图片拖动的步骤 HTML 结构 在 HTML 中创建一个可拖动的图片元素,确保设置 draggable 属性为 true。 <img id="draggable-img" src="ima…

vue实现拖动刻度

vue实现拖动刻度

实现拖动刻度功能 在Vue中实现拖动刻度功能可以通过结合原生HTML5的拖拽API和Vue的数据绑定特性来完成。以下是一个完整的实现方案: 基本实现思路 创建一个可拖动的滑块元素 监听鼠标事件处理…