当前位置:首页 > VUE

vue实现左右拉伸

2026-02-10 18:16:03VUE

实现左右拉伸布局的方法

在Vue中实现左右拉伸布局通常涉及CSS的弹性布局(Flexbox)或网格布局(Grid),结合Vue的响应式特性。以下是几种常见实现方式:

使用Flexbox布局

通过CSS的display: flex实现左右分栏,并允许拖动调整宽度。

<template>
  <div class="container">
    <div class="left-panel" ref="leftPanel" :style="{ width: leftWidth + 'px' }">
      左侧内容
    </div>
    <div class="divider" @mousedown="startDrag"></div>
    <div class="right-panel">
      右侧内容
    </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>
.container {
  display: flex;
  height: 100vh;
}
.left-panel {
  background: #f0f0f0;
  min-width: 100px;
  max-width: 80%;
}
.divider {
  width: 5px;
  background: #ddd;
  cursor: col-resize;
}
.right-panel {
  flex: 1;
  background: #fff;
}
</style>

使用Vue组件库

若需快速实现,可借助第三方库如vue-splitpanes

npm install vue-splitpanes
<template>
  <splitpanes>
    <pane min-size="20">左侧内容</pane>
    <pane>右侧内容</pane>
  </splitpanes>
</template>

<script>
import { Splitpanes, Pane } from 'vue-splitpanes';
export default {
  components: { Splitpanes, Pane }
};
</script>

<style>
@import 'vue-splitpanes/dist/vue-splitpanes.css';
</style>

使用CSS Grid布局

通过网格布局实现动态调整列宽:

vue实现左右拉伸

<template>
  <div class="grid-container" ref="container">
    <div class="left" :style="{ gridTemplateColumns: `${leftWidth}px 5px auto` }">
      <div>左侧内容</div>
      <div class="divider" @mousedown="startDrag"></div>
      <div>右侧内容</div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      leftWidth: 200
    };
  },
  methods: {
    startDrag(e) {
      const moveHandler = (e) => {
        this.leftWidth = e.clientX;
      };
      const upHandler = () => {
        document.removeEventListener('mousemove', moveHandler);
      };
      document.addEventListener('mousemove', moveHandler);
      document.addEventListener('mouseup', upHandler);
    }
  }
};
</script>

<style>
.grid-container {
  height: 100vh;
}
.left {
  display: grid;
  height: 100%;
}
.divider {
  background: #ddd;
  cursor: col-resize;
}
</style>

注意事项

  • 拖动性能优化:可节流mousemove事件避免频繁渲染。
  • 移动端适配:需添加触摸事件(touchstart/touchmove)。
  • 边界处理:限制最小/最大宽度防止面板过小或过大。

以上方法可根据实际需求选择,Flexbox方案适合简单场景,组件库适合快速开发,Grid方案则提供更灵活的布局控制。

标签: vue
分享给朋友:

相关文章

vue请求实现进度条

vue请求实现进度条

实现请求进度条的方法 在Vue中实现请求进度条可以通过多种方式完成,常见的有使用第三方库如axios结合nprogress或自定义进度条组件。以下是几种实现方法: 使用axios和nprogress…

vue实现slot

vue实现slot

Vue 中的 Slot 实现方法 Vue 的 slot 是一种内容分发机制,允许父组件向子组件插入内容。以下是几种常见的 Slot 实现方式: 默认 Slot 子组件通过 <slot>…

vue实现插件

vue实现插件

Vue 插件实现方法 Vue 插件通常用于为 Vue 应用添加全局功能或共享功能。以下是实现 Vue 插件的核心步骤: 插件基本结构 Vue 插件需要暴露一个 install 方法,该方法接收 Vu…

vue 实现vshow

vue 实现vshow

Vue 实现 v-show 在 Vue 中,v-show 是一个常用的指令,用于根据条件控制元素的显示或隐藏。与 v-if 不同,v-show 不会移除 DOM 元素,而是通过 CSS 的 displ…

vue实现vmodel

vue实现vmodel

Vue 实现 v-model Vue 的 v-model 是一个语法糖,用于在表单元素或组件上实现双向数据绑定。以下是其实现方式和原理的详细说明。 在表单元素上使用 v-model v-model…

vue实现双击

vue实现双击

Vue 实现双击事件的方法 在Vue中实现双击事件可以通过以下几种方式完成,具体选择取决于项目需求和开发习惯。 使用 @dblclick 指令 Vue提供了内置的@dblclick指令,可以…