当前位置:首页 > 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 3 实现的简单登录功能示例,包含表单验证和基础交互逻辑: 创建 Vue 组件 <template> <div class="login-containe…

vue实现ai

vue实现ai

Vue 实现 AI 功能 Vue.js 可以通过集成第三方 AI 服务或本地 AI 模型来实现 AI 功能。以下是几种常见的方法: 集成第三方 AI API 使用 Vue 调用如 OpenAI、Go…

vue评分实现

vue评分实现

Vue 评分组件实现方法 使用第三方库(如 Element UI) 安装 Element UI: npm install element-ui 引入并注册组件: import Vue from…

vue router 实现

vue router 实现

Vue Router 的实现 Vue Router 是 Vue.js 的官方路由管理器,用于构建单页面应用(SPA)。以下是 Vue Router 的核心实现方式。 安装 Vue Router 通过…

vue筛选实现

vue筛选实现

Vue 筛选实现方法 使用计算属性实现筛选 计算属性是 Vue 中实现数据筛选的常用方式。通过定义一个计算属性,可以根据输入的条件动态过滤数据。 <template> <div…

vue实现颜色

vue实现颜色

Vue 实现颜色的方法 在 Vue 中实现颜色管理可以通过多种方式,以下是一些常见的方法: 使用 CSS 变量动态绑定颜色 在 Vue 组件的样式中定义 CSS 变量,并通过 Vue 的数据绑定动态…