当前位置:首页 > VUE

vue实现上下分栏

2026-02-19 17:27:20VUE

Vue 实现上下分栏布局

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

方法一:使用 CSS Flexbox 布局

vue实现上下分栏

<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 布局

vue实现上下分栏

<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 组件实现可调整分栏

<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组件的data中定义好友列表数据,通常是一个数组对象,每个对象包含好友的基本信息如ID、头像、昵称等。 data() { return { f…

vue前端实现打印功能

vue前端实现打印功能

使用Vue实现前端打印功能 在Vue项目中实现打印功能可以通过多种方式完成,以下是几种常见的方法: 使用window.print()方法 这是最简单的打印方式,直接调用浏览器的打印功能。 meth…

vue实现iframe

vue实现iframe

Vue 中实现 iframe 的方法 在 Vue 项目中,可以通过多种方式实现 iframe 的嵌入。以下是几种常见的方法: 使用原生 HTML iframe 标签 直接在 Vue 组件的模板中使用…

vue菜单实现

vue菜单实现

Vue 菜单实现方法 使用 Element UI 的菜单组件 Element UI 提供了现成的菜单组件,适合快速实现导航菜单。安装 Element UI 后,可以直接使用 el-menu 相关组件。…

vue的实现

vue的实现

Vue 的实现原理 Vue 是一个渐进式 JavaScript 框架,其核心实现主要包括响应式系统、虚拟 DOM、模板编译和组件化机制。以下是 Vue 实现的主要技术细节。 响应式系统 Vue 的响…

vue实现ppt

vue实现ppt

Vue 实现 PPT 的功能 Vue 可以结合第三方库或自定义组件实现类似 PPT 的演示效果。以下是几种常见的方法: 使用第三方库 reveal.js reveal.js 是一个流行的演示框架,支…