当前位置:首页 > VUE

vue实现上下布局

2026-01-08 07:31:53VUE

实现上下布局的基本结构

在Vue中实现上下布局,可以通过CSS Flexbox或Grid布局快速完成。以下是一个基础模板示例:

<template>
  <div class="layout-container">
    <header class="header">顶部区域</header>
    <main class="main-content">主要内容区域</main>
  </div>
</template>

<style scoped>
.layout-container {
  display: flex;
  flex-direction: column;
  height: 100vh;
}
.header {
  height: 60px;
  background: #333;
  color: white;
}
.main-content {
  flex: 1;
  background: #f5f5f5;
}
</style>

固定顶部与滚动内容

若需要顶部固定、内容区域可滚动,可添加以下样式调整:

.layout-container {
  overflow: hidden;
}
.main-content {
  overflow-y: auto;
}

使用CSS Grid布局

另一种实现方式是使用CSS Grid:

<template>
  <div class="grid-layout">
    <div class="top-section">顶部</div>
    <div class="content-section">内容</div>
  </div>
</template>

<style scoped>
.grid-layout {
  display: grid;
  grid-template-rows: 80px 1fr;
  height: 100vh;
}
.top-section {
  background: #42b983;
}
.content-section {
  background: #f9f9f9;
}
</style>

响应式调整

添加媒体查询适应不同屏幕尺寸:

@media (max-width: 768px) {
  .header {
    height: 50px;
  }
}

带底部栏的布局

扩展为三部分布局(顶-中-底):

<template>
  <div class="triple-layout">
    <header>头部</header>
    <main>内容</main>
    <footer>底部</footer>
  </div>
</template>

<style scoped>
.triple-layout {
  display: grid;
  grid-template-rows: auto 1fr auto;
  min-height: 100vh;
}
footer {
  height: 40px;
  background: #ddd;
}
</style>

动态高度控制

通过Vue数据绑定动态调整布局高度:

<template>
  <div class="dynamic-layout" :style="{ '--header-height': headerHeight + 'px' }">
    <div class="dynamic-header">可调整头部</div>
    <div class="dynamic-main">内容</div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      headerHeight: 80
    }
  }
}
</script>

<style scoped>
.dynamic-layout {
  --header-height: 80px;
  display: flex;
  flex-direction: column;
  height: 100vh;
}
.dynamic-header {
  height: var(--header-height);
}
.dynamic-main {
  height: calc(100vh - var(--header-height));
}
</style>

vue实现上下布局

标签: 布局上下
分享给朋友:

相关文章

vue实现左右布局

vue实现左右布局

实现左右布局的方法 在Vue中实现左右布局可以通过多种方式完成,以下是几种常见的方法: 使用Flexbox布局 Flexbox是一种现代的CSS布局方式,可以轻松实现左右布局。 <t…

vue如何实现两栏布局

vue如何实现两栏布局

使用Flexbox实现两栏布局 Flexbox是CSS3中强大的布局方式,可以轻松实现两栏布局。在Vue中可以直接在组件的style标签中使用。 <template> <di…

elementui响应式布局

elementui响应式布局

响应式布局基础概念 响应式布局指页面能够根据屏幕尺寸自动调整结构和样式,确保在不同设备上呈现良好的用户体验。Element UI 基于 Vue.js,其组件默认支持响应式设计,但需结合 CSS 媒体查…

uniapp布局样式

uniapp布局样式

uniapp布局样式基础 uniapp基于Vue.js框架,支持多种布局方式,包括Flex布局、Grid布局和传统盒模型布局。样式编写遵循CSS规范,同时支持rpx响应式单位。 Flex布局示例…

uniapp布局规范

uniapp布局规范

uniapp布局规范 uniapp的布局规范基于Flexbox模型,支持跨平台开发,需兼顾不同设备的适配性。以下是核心布局要点: Flex布局基础 使用Flexbox实现弹性布局,默认displa…