当前位置:首页 > VUE

vue 实现左右分栏

2026-03-08 15:17:11VUE

实现左右分栏的基本结构

使用 Vue 实现左右分栏布局,可以通过 Flexbox 或 Grid 布局结合组件拆分完成。以下是一个基于 Flexbox 的示例:

<template>
  <div class="split-container">
    <div class="left-panel">
      <!-- 左侧内容 -->
      <slot name="left"></slot>
    </div>
    <div class="right-panel">
      <!-- 右侧内容 -->
      <slot name="right"></slot>
    </div>
  </div>
</template>

<script>
export default {
  name: 'SplitLayout'
}
</script>

<style scoped>
.split-container {
  display: flex;
  height: 100vh;
}
.left-panel {
  flex: 1;
  background: #f0f0f0;
}
.right-panel {
  flex: 1;
  background: #e0e0e0;
}
</style>

添加可调整的分栏宽度

通过拖动分隔线调整左右分栏宽度,需监听鼠标事件:

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

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

<style scoped>
.split-container {
  display: flex;
  height: 100vh;
}
.left-panel {
  background: #f0f0f0;
}
.right-panel {
  flex: 1;
  background: #e0e0e0;
}
.divider {
  width: 5px;
  background: #ccc;
  cursor: col-resize;
}
</style>

响应式分栏布局

添加媒体查询实现移动端适配:

vue 实现左右分栏

@media (max-width: 768px) {
  .split-container {
    flex-direction: column;
  }
  .left-panel, .right-panel {
    width: 100% !important;
  }
  .divider {
    width: 100%;
    height: 5px;
    cursor: row-resize;
  }
}

使用第三方库

若需更复杂功能(如嵌套分栏),可考虑以下库:

  • vue-splitpane:提供预构建的分栏组件
  • split.js:轻量级的分栏控制库

安装 vue-splitpane 的示例:

vue 实现左右分栏

npm install vue-splitpane

使用方式:

<template>
  <split-pane :min-percent="20" :default-percent="30" split="vertical">
    <template #paneL>左侧内容</template>
    <template #paneR>右侧内容</template>
  </split-pane>
</template>

<script>
import SplitPane from 'vue-splitpane'
export default {
  components: { SplitPane }
}
</script>

性能优化建议

对于内容复杂的分栏:

  • 使用 v-ifkeep-alive 控制子组件渲染
  • 对静态内容使用 v-once 指令
  • 避免在分栏内使用重型计算属性

标签: vue
分享给朋友:

相关文章

在线脑图 vue 实现

在线脑图 vue 实现

在线脑图 Vue 实现方案 使用开源库实现 推荐使用 mind-elixir 或 jsmind 这类开源库,它们专为脑图设计且支持 Vue 集成。 安装依赖: npm install mind-e…

vue实现计时

vue实现计时

Vue 实现计时功能 在 Vue 中实现计时功能可以通过多种方式完成,以下是几种常见的方法: 使用 setInterval 实现基础计时器 通过 setInterval 和 clearInterva…

vue实现方法

vue实现方法

Vue 实现方法 Vue 是一种流行的前端框架,用于构建用户界面和单页应用。以下是几种常见的 Vue 实现方法: 创建 Vue 实例 通过 new Vue() 创建一个 Vue 实例,传入配置对象,…

vue实现bootstrap

vue实现bootstrap

Vue 中集成 Bootstrap 的方法 在 Vue 项目中集成 Bootstrap 可以通过以下方式实现,涵盖样式、组件和交互功能。 安装 Bootstrap 依赖 通过 npm 或 yarn…

vue实现海报

vue实现海报

Vue 实现海报生成方案 使用 html2canvas 库 html2canvas 是一个将 HTML 元素转换为 Canvas 的库,适合将 Vue 组件渲染为海报图片 安装依赖: npm in…

vue实现前端

vue实现前端

Vue 实现前端的核心方法与步骤 安装 Vue 项目 使用 Vue CLI 或 Vite 创建项目。Vue CLI 是传统构建工具,Vite 是新一代轻量级工具。 npm init vue@la…