当前位置:首页 > VUE

vue实现上下分栏

2026-02-19 17:27:20VUE

Vue 实现上下分栏布局

上下分栏布局通常指将页面垂直划分为两个部分,可以通过以下方法实现:

方法一:使用 CSS Flexbox 布局

<template>
  <div class="container">
    <div class="top-section">顶部内容区域</div>
    <div class="bottom-section">底部内容区域</div>
  </div>
</template>

<style scoped>
.container {
  display: flex;
  flex-direction: column;
  height: 100vh;
}
.top-section {
  flex: 1;
  background-color: #f0f0f0;
}
.bottom-section {
  flex: 1;
  background-color: #e0e0e0;
}
</style>

方法二:使用 CSS Grid 布局

<template>
  <div class="grid-container">
    <div class="header">头部区域</div>
    <div class="content">主要内容区域</div>
  </div>
</template>

<style scoped>
.grid-container {
  display: grid;
  grid-template-rows: 100px 1fr;
  height: 100vh;
}
.header {
  background-color: #ddd;
}
.content {
  background-color: #eee;
}
</style>

方法三:使用百分比高度

<template>
  <div>
    <div style="height: 30%; background: #f5f5f5;">上部分</div>
    <div style="height: 70%; background: #e5e5e5;">下部分</div>
  </div>
</template>

方法四:使用 Vue 组件实现可调整分栏

vue实现上下分栏

<template>
  <div class="resizable-container">
    <div 
      class="top-panel" 
      :style="{ height: topHeight + 'px' }"
    >
      可调整的顶部面板
    </div>
    <div class="divider" @mousedown="startResize"></div>
    <div class="bottom-panel">
      底部面板
    </div>
  </div>
</template>

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

<style scoped>
.resizable-container {
  height: 100vh;
  position: relative;
}
.top-panel {
  background: #f0f0f0;
  overflow: auto;
}
.bottom-panel {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  top: 200px;
  background: #e0e0e0;
}
.divider {
  height: 5px;
  background: #ccc;
  cursor: row-resize;
}
</style>

注意事项

  • 确保父容器有明确的高度设置,否则分栏可能无法正确显示
  • 考虑添加 overflow 属性处理内容溢出的情况
  • 响应式设计可以通过媒体查询调整分栏比例
  • 复杂场景可考虑使用第三方布局库如 Split.js

以上方法可根据具体需求选择使用,Flexbox 和 Grid 方案较为现代且推荐使用,百分比方案简单直接,可调整分栏方案提供更好的用户体验。

标签: 上下vue
分享给朋友:

相关文章

vue如何实现冒泡

vue如何实现冒泡

Vue 实现冒泡排序 在 Vue 中实现冒泡排序,可以通过数据绑定和响应式特性动态展示排序过程。以下是一个完整的实现示例,包含代码和详细说明。 数据准备 在 Vue 的 data 中定义一个数组作为…

vue实现下载暂停

vue实现下载暂停

Vue实现下载暂停功能 在Vue中实现下载暂停功能,通常需要结合XMLHttpRequest或Fetch API的AbortController来控制请求中断。以下是具体实现方法: 使用XMLHtt…

vue实现一个标签选择

vue实现一个标签选择

Vue 标签选择组件实现 基础实现 使用 Vue 的 v-for 和 v-model 指令可以快速实现标签选择功能: <template> <div class="tag-se…

vue实现pie

vue实现pie

Vue 实现饼图 在 Vue 中实现饼图通常可以通过第三方图表库(如 ECharts、Chart.js 或 Highcharts)来完成。以下是使用 ECharts 和 Chart.js 的两种常见方…

vue observer实现

vue observer实现

Vue Observer 实现原理 Vue 的响应式系统核心是通过 Object.defineProperty(Vue 2)或 Proxy(Vue 3)实现的 Observer 模式。以下是关键实现细…

实现vue table

实现vue table

Vue 表格实现方法 基础表格实现 使用 Vue 的 v-for 指令可以快速渲染表格数据。在模板中定义表格结构,通过数据绑定动态生成行和列。 <template> <tabl…