当前位置:首页 > 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 单文件组件示例:

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>

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

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

相关文章

elementui边框

elementui边框

修改Element UI组件边框样式 通过CSS覆盖默认样式可以修改Element UI组件的边框。以下方法适用于修改输入框、按钮、表格等组件的边框颜色、粗细或样式。 .el-input__inne…

vue实现拖动栏

vue实现拖动栏

Vue 实现拖动栏 使用 HTML5 拖放 API HTML5 提供了原生的拖放 API,可以直接在 Vue 中使用。通过 draggable 属性标记可拖动元素,并监听 dragstart、drag…

vue 实现拖动效果

vue 实现拖动效果

使用 Vue 实现拖动效果 Vue 提供了多种方式实现拖动效果,可以通过原生 HTML5 的拖放 API,也可以使用第三方库如 vuedraggable。以下是两种常见的方法: 使用 HTML5 拖…

vue实现卡片拖动排序

vue实现卡片拖动排序

Vue 实现卡片拖动排序的方法 使用 Vue 实现卡片拖动排序可以通过原生 HTML5 的拖放 API 或第三方库如 vuedraggable 来实现。以下是两种方法的详细说明。 使用 HTML5…

vue实现轮播图拖动

vue实现轮播图拖动

Vue 实现轮播图拖动功能 实现轮播图拖动功能需要结合 Vue 的响应式数据和触摸事件(或鼠标事件)。以下是具体实现方法: 基础轮播图结构 使用 Vue 的 v-for 渲染轮播项,并通过 CSS…

react实现拖动排序

react实现拖动排序

实现拖动排序的基本思路 在React中实现拖动排序通常借助第三方库如react-beautiful-dnd或react-dnd。这两种库提供了完整的拖放API,适合列表、看板等场景的排序需求。 使用…