当前位置:首页 > VUE

vue实现边框高度拖动

2026-02-22 19:31:34VUE

Vue 实现边框高度拖动

实现边框高度拖动的功能通常涉及监听鼠标事件并动态调整元素的高度。以下是实现步骤:

创建可拖动边框的组件

在 Vue 中创建一个组件,包含一个可拖动的边框和内容区域。边框可以是水平或垂直的,拖动时调整相邻区域的高度或宽度。

<template>
  <div class="resizable-container">
    <div class="top-pane" :style="{ height: topHeight + 'px' }">
      <!-- 顶部内容区域 -->
    </div>
    <div 
      class="resizer" 
      @mousedown="startResize"
    ></div>
    <div class="bottom-pane" :style="{ height: bottomHeight + 'px' }">
      <!-- 底部内容区域 -->
    </div>
  </div>
</template>

处理鼠标事件

在组件中定义方法,处理鼠标按下、移动和释放事件,动态调整区域高度。

<script>
export default {
  data() {
    return {
      topHeight: 200,
      bottomHeight: 200,
      isResizing: false,
      startY: 0
    };
  },
  methods: {
    startResize(e) {
      this.isResizing = true;
      this.startY = e.clientY;
      document.addEventListener('mousemove', this.resize);
      document.addEventListener('mouseup', this.stopResize);
    },
    resize(e) {
      if (!this.isResizing) return;
      const dy = e.clientY - this.startY;
      this.topHeight += dy;
      this.bottomHeight -= dy;
      this.startY = e.clientY;
    },
    stopResize() {
      this.isResizing = false;
      document.removeEventListener('mousemove', this.resize);
      document.removeEventListener('mouseup', this.stopResize);
    }
  }
};
</script>

添加样式

为组件添加样式,确保边框可拖动且视觉效果明显。

<style>
.resizable-container {
  display: flex;
  flex-direction: column;
  height: 100%;
}

.top-pane, .bottom-pane {
  overflow: hidden;
}

.resizer {
  height: 5px;
  background-color: #ccc;
  cursor: row-resize;
}

.resizer:hover {
  background-color: #999;
}
</style>

优化拖动体验

为防止拖动时文本被选中或页面滚动,可以添加额外的样式和逻辑。

methods: {
  startResize(e) {
    this.isResizing = true;
    this.startY = e.clientY;
    document.body.style.userSelect = 'none'; // 防止文本选中
    document.addEventListener('mousemove', this.resize);
    document.addEventListener('mouseup', this.stopResize);
  },
  stopResize() {
    this.isResizing = false;
    document.body.style.userSelect = '';
    document.removeEventListener('mousemove', this.resize);
    document.removeEventListener('mouseup', this.stopResize);
  }
}

支持双向绑定

如果需要通过父组件控制高度,可以使用 v-modelprops$emit

props: {
  initialTopHeight: {
    type: Number,
    default: 200
  }
},
data() {
  return {
    topHeight: this.initialTopHeight,
    bottomHeight: 400 - this.initialTopHeight
  };
},
watch: {
  topHeight(newVal) {
    this.$emit('update:topHeight', newVal);
  }
}

完整示例

以下是一个完整的 Vue 单文件组件示例:

<template>
  <div class="resizable-container">
    <div class="top-pane" :style="{ height: topHeight + 'px' }">
      <p>顶部区域,高度: {{ topHeight }}px</p>
    </div>
    <div 
      class="resizer" 
      @mousedown="startResize"
    ></div>
    <div class="bottom-pane" :style="{ height: bottomHeight + 'px' }">
      <p>底部区域,高度: {{ bottomHeight }}px</p>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      topHeight: 200,
      bottomHeight: 200,
      isResizing: false,
      startY: 0
    };
  },
  methods: {
    startResize(e) {
      this.isResizing = true;
      this.startY = e.clientY;
      document.body.style.userSelect = 'none';
      document.addEventListener('mousemove', this.resize);
      document.addEventListener('mouseup', this.stopResize);
    },
    resize(e) {
      if (!this.isResizing) return;
      const dy = e.clientY - this.startY;
      this.topHeight += dy;
      this.bottomHeight -= dy;
      this.startY = e.clientY;
    },
    stopResize() {
      this.isResizing = false;
      document.body.style.userSelect = '';
      document.removeEventListener('mousemove', this.resize);
      document.removeEventListener('mouseup', this.stopResize);
    }
  }
};
</script>

<style>
.resizable-container {
  display: flex;
  flex-direction: column;
  height: 400px;
  border: 1px solid #ddd;
}

.top-pane, .bottom-pane {
  overflow: auto;
  padding: 10px;
}

.resizer {
  height: 5px;
  background-color: #ccc;
  cursor: row-resize;
}

.resizer:hover {
  background-color: #999;
}
</style>

通过以上步骤,可以实现一个简单的边框高度拖动功能。根据需求,可以进一步扩展为支持多个区域、横向拖动或更复杂的布局。

vue实现边框高度拖动

标签: 拖动边框
分享给朋友:

相关文章

vue实现数据拖动

vue实现数据拖动

Vue 实现数据拖动 在 Vue 中实现数据拖动通常涉及 HTML5 的拖放 API 或第三方库(如 vuedraggable)。以下是两种常见方法: 使用 HTML5 拖放 API 设置拖拽元素…

vue实现表格拖动列宽

vue实现表格拖动列宽

实现表格列宽拖动的核心思路 通过监听鼠标事件(mousedown、mousemove、mouseup)实现拖拽逻辑,动态修改列宽。需要处理表头与表体的联动,确保拖动时视觉一致。 基础HTML结构…

vue 实现边框闪烁

vue 实现边框闪烁

实现边框闪烁的几种方法 使用CSS动画 通过CSS的@keyframes定义动画效果,结合Vue的v-bind:class动态绑定类名实现边框闪烁效果。 <template> &l…

vue实现组件拖动

vue实现组件拖动

Vue 实现组件拖动的几种方法 使用 HTML5 拖放 API HTML5 原生提供了拖放 API,可以通过 draggable 属性实现基础拖拽功能。在 Vue 中可以通过事件绑定实现交互逻辑。…

vue实现位移拖动

vue实现位移拖动

Vue 实现位移拖动的方法 使用鼠标事件实现拖动 在 Vue 中可以通过监听鼠标事件实现元素的位移拖动。需要监听 mousedown、mousemove 和 mouseup 事件来跟踪拖动过程。 &…

vue实现拖动文件移动

vue实现拖动文件移动

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