当前位置:首页 > 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

使用方式:

vue实现上下分栏

<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实现slider

vue实现slider

Vue 实现 Slider 组件 使用 Vue 实现 Slider 组件可以通过原生开发或借助第三方库。以下是两种常见方法: 原生实现 Slider 创建一个基础的 Slider 组件,通过 v-m…

vue实现socket

vue实现socket

Vue 中实现 WebSocket 通信 在 Vue 项目中实现 WebSocket 通信可以通过原生 WebSocket API 或第三方库(如 socket.io-client)完成。以下是两种常…

vue实现webapp

vue实现webapp

Vue 实现 WebApp 的基本步骤 环境准备 确保已安装 Node.js 和 npm/yarn。通过 Vue CLI 或 Vite 创建项目: npm install -g @vue/cli v…

vue实现选择

vue实现选择

Vue 实现选择功能的方法 在 Vue 中实现选择功能可以通过多种方式完成,以下介绍几种常见的实现方法。 使用 v-model 绑定单选 通过 v-model 可以轻松实现单选功能。以下是一个简单的…

vue实现api

vue实现api

Vue 实现 API 调用 在 Vue 中调用 API 通常涉及使用 axios 或 fetch 发送 HTTP 请求,并结合 Vue 的响应式特性管理数据。以下是常见的实现方式: 安装 axios…

vue实现系统

vue实现系统

Vue 实现系统的核心方法 使用 Vue.js 构建系统时,需结合其响应式特性与组件化开发模式。以下为关键实现路径: 项目初始化与工程配置 通过 Vue CLI 或 Vite 创建项目骨架,推荐选择…