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

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

<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实现图片搜索

Vue实现图片搜索功能 使用HTML5的File API获取图片 通过<input type="file">元素让用户选择图片文件,使用FileReader对象读取图片数据。 <t…

vue实现过程

vue实现过程

Vue 的实现过程 Vue 的实现过程可以分为多个核心模块,包括响应式系统、虚拟 DOM、模板编译等。以下是 Vue 的主要实现过程分析。 响应式系统 Vue 的响应式系统基于 Object.de…

vue SSG实现

vue SSG实现

Vue SSG 实现方法 Vue 的静态站点生成(SSG)可以通过多种工具和框架实现,以下是几种常见的方法: 使用 VuePress VuePress 是一个基于 Vue 的静态站点生成器,适合文档…

分页实现vue

分页实现vue

分页实现(Vue) 在Vue中实现分页功能通常需要结合前端分页逻辑和后端API支持。以下是两种常见的实现方式: 前端分页实现 适用于数据量较小的情况,直接在客户端完成分页逻辑。 <tem…

vue实现表白

vue实现表白

Vue 实现表白页面 使用 Vue 可以快速创建一个动态、交互式的表白页面。以下是一个简单的实现方案: 基础结构 创建一个 Vue 项目或单文件组件,包含以下核心部分: <template&…

vue实现toast

vue实现toast

Vue 实现 Toast 的方法 使用第三方库(推荐) 对于快速实现 Toast 功能,推荐使用成熟的第三方库如 vue-toastification 或 vant 的 Toast 组件。 安装…