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

可调整宽度的分栏

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

<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-split-panel:专门用于创建可调整的分割面板
  • element-uiel-container布局组件
  • vuetifyv-container网格系统

安装vue-split-panel的示例:

npm install vue-split-panel

使用示例:

vue 实现左右分栏

<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 模板的基本结构 Vue 模板基于 HTML 语法,通过 Vue 的指令和插值实现动态渲染。以下是一个基础模板示例: <template> <div id="app"…

vue实现数组

vue实现数组

Vue 实现数组操作的方法 在 Vue 中,数组的操作需要特别注意响应式的问题。Vue 提供了一些方法来确保数组的变动能够触发视图更新。 响应式数组方法 Vue 对以下数组方法进行了封装,确保它们能…

vue实现心电图

vue实现心电图

Vue 实现心电图效果 在 Vue 中实现心电图效果通常需要结合 Canvas 或 SVG 进行动态绘图,模拟心电图数据的实时变化。以下是两种实现方式的核心思路和代码示例: 使用 Canvas 绘制…

vue实现addclass

vue实现addclass

Vue 实现动态添加 class 的方法 在 Vue 中动态添加 class 可以通过多种方式实现,以下是常见的几种方法: 使用对象语法 通过绑定一个对象到 :class,可以动态切换 class…

vue实现刷卡

vue实现刷卡

Vue 实现刷卡效果 在 Vue 中实现刷卡效果可以通过 CSS 动画和 Vue 的过渡系统结合完成。以下是一种常见的实现方式: 基础实现方法 安装 Vue 过渡依赖(如果尚未安装): npm i…

vue实现打印

vue实现打印

使用Vue实现打印功能 在Vue项目中实现打印功能可以通过多种方式完成,以下是几种常见的方法: 方法一:使用window.print() 这种方法适用于打印整个页面或特定区域的内容。 // 在Vu…