当前位置:首页 > VUE

vue 实现页面分栏

2026-01-19 10:56:43VUE

使用 CSS Grid 布局实现分栏

通过 CSS Grid 可以快速实现分栏布局。在 Vue 模板中定义网格容器,设置 grid-template-columns 控制栏数和宽度比例。

<template>
  <div class="grid-container">
    <div class="left-column">左侧内容</div>
    <div class="right-column">右侧内容</div>
  </div>
</template>

<style scoped>
.grid-container {
  display: grid;
  grid-template-columns: 1fr 2fr; /* 左右比例 1:2 */
  gap: 16px; /* 栏间距 */
}
</style>

使用 Flexbox 实现动态分栏

Flexbox 适合需要动态调整的分栏场景。通过 flex 属性控制各栏的伸缩比例。

vue 实现页面分栏

<template>
  <div class="flex-container">
    <div class="sidebar" :style="{ width: sidebarWidth + 'px' }">侧边栏</div>
    <div class="main-content">主内容区</div>
  </div>
</template>

<script>
export default {
  data() {
    return { sidebarWidth: 200 }
  }
}
</script>

<style scoped>
.flex-container {
  display: flex;
}
.sidebar {
  flex-shrink: 0; /* 禁止收缩 */
}
.main-content {
  flex-grow: 1; /* 自动填充剩余空间 */
}
</style>

响应式分栏设计

结合媒体查询实现移动端和桌面端的不同分栏表现:

vue 实现页面分栏

/* 桌面端两栏布局 */
@media (min-width: 768px) {
  .responsive-container {
    display: grid;
    grid-template-columns: 250px 1fr;
  }
}

/* 移动端单栏堆叠 */
@media (max-width: 767px) {
  .responsive-container {
    display: block;
  }
}

可拖拽调整的分栏实现

通过 vue-draggable 或原生拖拽 API 实现栏宽调整功能:

<template>
  <div class="draggable-container">
    <div class="left-panel" :style="{ width: leftWidth + 'px' }"></div>
    <div 
      class="divider"
      @mousedown="startDrag"
    ></div>
    <div class="right-panel"></div>
  </div>
</template>

<script>
export default {
  data() {
    return { leftWidth: 300, isDragging: false }
  },
  methods: {
    startDrag(e) {
      this.isDragging = true
      document.addEventListener('mousemove', this.handleDrag)
      document.addEventListener('mouseup', this.stopDrag)
    },
    handleDrag(e) {
      if (this.isDragging) {
        this.leftWidth = e.clientX
      }
    },
    stopDrag() {
      this.isDragging = false
    }
  }
}
</script>

<style>
.draggable-container {
  display: flex;
  height: 100vh;
}
.divider {
  width: 8px;
  background: #ddd;
  cursor: col-resize;
}
</style>

基于组件的分栏封装

创建可复用的分栏组件,通过插槽分配内容区域:

<!-- SplitPane.vue -->
<template>
  <div class="split-pane">
    <div class="pane left-pane">
      <slot name="left"></slot>
    </div>
    <div class="pane right-pane">
      <slot name="right"></slot>
    </div>
  </div>
</template>

<!-- 使用示例 -->
<SplitPane>
  <template #left>
    <Navigation/>
  </template>
  <template #right>
    <MainContent/>
  </template>
</SplitPane>

标签: 页面vue
分享给朋友:

相关文章

vue搜索过后实现分页

vue搜索过后实现分页

Vue 实现搜索后分页功能 数据绑定与搜索逻辑 在 Vue 组件中定义必要的数据属性: data() { return { searchQuery: '', currentPag…

vue实现排序

vue实现排序

Vue 实现排序的方法 在 Vue 中实现排序可以通过多种方式完成,以下是一些常见的方法: 使用计算属性排序数组 计算属性非常适合对数据进行排序,因为它会在依赖的数据变化时自动更新。以下是一个示例:…

vue grid实现

vue grid实现

Vue Grid 实现方法 在 Vue 中实现网格布局可以通过多种方式完成,包括使用原生 CSS Grid、第三方组件库或自定义组件。以下是几种常见方法: 使用原生 CSS Grid 通过 Vue…

vue tab实现

vue tab实现

Vue Tab 实现方法 使用动态组件实现 Tab 在 Vue 中可以通过动态组件 <component :is="currentTab"> 结合 v-for 和 v-bind 实现 Ta…

vue实现标题

vue实现标题

Vue 实现标题的方法 在Vue中实现标题可以通过多种方式,以下是几种常见的方法: 方法一:使用模板语法 在Vue组件的模板中直接使用HTML的<h1>到<h6>标签来定义…

vue 绑定实现

vue 绑定实现

Vue 绑定实现 Vue 提供了多种数据绑定的方式,包括文本插值、属性绑定、事件绑定、双向绑定等。以下是常见的绑定实现方法: 文本插值 使用双大括号 {{ }} 进行文本插值,将数据动态渲染到 DO…