当前位置:首页 > 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 实现拖动图片验证功能 核心思路 拖动图片验证通常需要用户将图片拖动到指定区域或按正确顺序排列。实现时需要处理拖拽事件、验证逻辑和视觉反馈。 实现步骤 1. 基础组件结构 <templ…

vue实现拖动

vue实现拖动

Vue 实现拖动的几种方法 使用 HTML5 原生拖放 API HTML5 提供了原生拖放 API,可以通过 draggable 属性、dragstart、dragend、dragover 和 dro…

vue拖动实现

vue拖动实现

Vue 拖动实现方法 在 Vue 中实现拖动功能可以通过多种方式完成,以下介绍几种常见的方法: 使用 HTML5 拖放 API HTML5 提供了原生的拖放 API,可以直接在 Vue 中使用。通…

vue如何实现拖动

vue如何实现拖动

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

jquery 宽度

jquery 宽度

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

vue实现数据拖动

vue实现数据拖动

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