当前位置:首页 > VUE

vue实现上下分栏

2026-01-19 01:03:22VUE

实现上下分栏的基本结构

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

<template>
  <div class="split-layout">
    <div class="top-panel">
      <!-- 上栏内容 -->
    </div>
    <div class="bottom-panel">
      <!-- 下栏内容 -->
    </div>
  </div>
</template>

<style scoped>
.split-layout {
  display: flex;
  flex-direction: column;
  height: 100vh; /* 视口高度 */
}

.top-panel {
  flex: 1; /* 可调整比例 */
  background: #f0f0f0;
}

.bottom-panel {
  flex: 1;
  background: #e0e0e0;
}
</style>

可调整的分栏比例

通过修改flex属性值控制分栏比例。例如7:3的分栏:

.top-panel {
  flex: 7;
}

.bottom-panel {
  flex: 3;
}

固定高度分栏

若需要固定某栏高度(如顶部固定100px):

.top-panel {
  height: 100px;
  flex: none; /* 禁用flex伸缩 */
}

.bottom-panel {
  flex: 1; /* 剩余空间 */
}

添加分割线交互

实现可拖拽调整分栏大小的功能:

<template>
  <div class="resizable-layout">
    <div class="top-panel" :style="{ height: topHeight + 'px' }">
      <div class="resizer" @mousedown="startResize"></div>
    </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) {
        this.topHeight = e.clientY
      }
    },
    stopResize() {
      this.isResizing = false
      document.removeEventListener('mousemove', this.resize)
    }
  }
}
</script>

<style>
.resizable-layout {
  position: relative;
  height: 100vh;
}

.resizer {
  height: 5px;
  background: #ccc;
  cursor: row-resize;
}
</style>

响应式处理

通过媒体查询适应不同屏幕尺寸:

@media (max-width: 768px) {
  .split-layout {
    flex-direction: column;
  }

  .top-panel, .bottom-panel {
    flex: none;
    height: auto;
  }
}

使用第三方组件

若需快速实现复杂分栏,可考虑以下库:

  • vue-split-panel:提供可拖拽调整的分割面板
  • vue-resizable:支持元素尺寸调整

安装示例:

npm install vue-split-panel

使用方式:

<template>
  <split-panel direction="vertical">
    <div slot="panel-1">上栏</div>
    <div slot="panel-2">下栏</div>
  </split-panel>
</template>

<script>
import { SplitPanel } from 'vue-split-panel'

export default {
  components: { SplitPanel }
}
</script>

vue实现上下分栏

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

相关文章

vue实现单页面

vue实现单页面

Vue 实现单页面应用(SPA) Vue.js 是一个渐进式框架,非常适合构建单页面应用(SPA)。SPA 的核心特点是整个应用只有一个 HTML 页面,通过动态加载内容实现页面切换,无需重新加载整个…

vue实现屏幕共享

vue实现屏幕共享

Vue 实现屏幕共享的方法 使用 WebRTC 和 getDisplayMedia API 通过 navigator.mediaDevices.getDisplayMedia 捕获屏幕内容,结合 We…

vue卖座网实现

vue卖座网实现

Vue 卖座网实现 项目结构搭建 使用 Vue CLI 快速初始化项目,安装必要依赖如 Vue Router、Vuex、Axios。 创建核心目录结构:components(通用组件)、views(页…

vue实现多选题

vue实现多选题

Vue实现多选题的方法 使用Vue实现多选题功能,可以通过v-model绑定数组、动态渲染选项、以及处理选中状态来实现。以下是一个完整的实现示例: 基础实现代码 <template>…

vue实现导航栏切图

vue实现导航栏切图

Vue 实现导航栏切图 使用 Vue Router 实现基础导航 在 Vue 项目中,可以通过 Vue Router 实现导航栏的路由切换。创建一个导航栏组件,并在其中使用 <router-li…

vue实现点击旋转轮盘

vue实现点击旋转轮盘

实现点击旋转轮盘效果 在Vue中实现点击旋转轮盘效果,可以通过CSS动画和Vue的数据绑定结合完成。以下是一个完整的实现方案: 准备工作 需要安装Vue.js环境,可以通过CDN引入或使用Vue…