当前位置:首页 > VUE

vue 实现左右分栏

2026-03-29 02:14:52VUE

实现左右分栏的基本结构

使用Vue实现左右分栏可以通过Flex布局或Grid布局快速完成。以下是一个基于Flex布局的示例代码模板:

<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: 'SplitPanel'
}
</script>

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

可调整宽度的分栏

如果需要允许用户拖动调整分栏宽度,可以结合拖拽事件实现:

vue 实现左右分栏

<template>
  <div class="resizable-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: 300,
      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>
.resizable-container {
  display: flex;
  height: 100vh;
}
.left-panel {
  width: 300px;
  background: #f5f5f5;
  overflow: auto;
  resize: none;
}
.divider {
  width: 8px;
  background: #ddd;
  cursor: col-resize;
}
.right-panel {
  flex: 1;
  background: #fff;
  overflow: auto;
}
</style>

响应式分栏布局

针对移动设备添加响应式设计,当屏幕宽度小于某个值时自动切换为上下布局:

@media (max-width: 768px) {
  .split-container {
    flex-direction: column;
  }
  .left-panel, .right-panel {
    width: 100% !important;
    flex: none;
  }
}

使用第三方组件库

对于更复杂的需求,可以考虑使用现成的Vue组件库:

vue 实现左右分栏

  • vue-split-panel:专门用于创建可调整的分割面板
  • element-uiel-container布局组件
  • vuetifyv-container网格系统

安装vue-split-panel的示例:

npm install vue-split-panel

使用示例:

<template>
  <split-panel>
    <split-panel-item :size="30">
      <slot name="left"></slot>
    </split-panel-item>
    <split-panel-item :size="70">
      <slot name="right"></slot>
    </split-panel-item>
  </split-panel>
</template>

<script>
import { SplitPanel, SplitPanelItem } from 'vue-split-panel'
export default {
  components: { SplitPanel, SplitPanelItem }
}
</script>

注意事项

  1. 分栏高度通常需要明确设置(如100vh或具体像素值),否则可能无法正常显示
  2. 内容溢出时建议添加overflow: auto保证可滚动
  3. 可拖动分隔条需要处理好鼠标事件,避免与其他交互冲突
  4. 响应式设计时要考虑不同设备下的用户体验差异

标签: vue
分享给朋友:

相关文章

vue弹幕实现不重叠

vue弹幕实现不重叠

实现 Vue 弹幕不重叠的方法 CSS 定位与动画控制 通过绝对定位和动态计算弹幕轨道高度,确保弹幕在不同轨道上运行。每条弹幕占据固定高度的轨道,避免重叠。 // 弹幕轨道高度计算 const tr…

vue实现缩放

vue实现缩放

Vue 实现缩放的方法 在 Vue 中实现缩放功能可以通过多种方式完成,以下是一些常见的方法: 使用 CSS transform 属性 通过 CSS 的 transform: scale() 属性可…

vue实现picker

vue实现picker

Vue 实现 Picker 组件的方法 在 Vue 中实现 Picker 组件可以通过多种方式完成,以下是几种常见的实现方法: 使用原生 HTML 和 CSS 实现 通过 Vue 的模板和样式绑定,…

vue多选实现

vue多选实现

Vue 多选实现方法 基础实现(v-model + multiple) 在 Vue 中实现多选可以通过 <select> 元素的 multiple 属性结合 v-model 完成。v-mo…

vue环境实现

vue环境实现

Vue 环境搭建 安装 Node.js 和 npm Node.js 是运行 Vue 的基础环境,npm 是包管理工具。从 Node.js 官网 下载并安装最新稳定版本。安装完成后,在终端运行以下命令验…

vue实现api

vue实现api

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