vue 实现分屏
Vue 实现分屏的方法
使用 CSS Flexbox 或 Grid 布局
在 Vue 中可以通过 CSS Flexbox 或 Grid 布局实现分屏效果。创建一个父容器,将子元素设置为弹性或网格布局,调整宽度或高度比例即可实现分屏。

<template>
<div class="split-screen">
<div class="left-pane">左侧内容</div>
<div class="right-pane">右侧内容</div>
</div>
</template>
<style>
.split-screen {
display: flex;
height: 100vh;
}
.left-pane {
flex: 1;
background: #f0f0f0;
}
.right-pane {
flex: 1;
background: #e0e0e0;
}
</style>
使用第三方库(如 Split.js)
Split.js 是一个轻量级的分屏库,可以方便地实现可拖拽的分屏效果。在 Vue 中可以通过引入 Split.js 来实现。

<template>
<div class="split-container">
<div id="left-pane">左侧内容</div>
<div id="right-pane">右侧内容</div>
</div>
</template>
<script>
import Split from 'split.js';
export default {
mounted() {
Split(['#left-pane', '#right-pane'], {
sizes: [50, 50],
minSize: 100,
});
},
};
</script>
<style>
.split-container {
display: flex;
height: 100vh;
}
#left-pane, #right-pane {
height: 100%;
overflow: auto;
}
</style>
动态调整分屏比例
通过 Vue 的数据绑定和计算属性,可以实现动态调整分屏比例的功能。
<template>
<div class="split-screen">
<div class="left-pane" :style="{ width: leftWidth + '%' }">左侧内容</div>
<div class="right-pane" :style="{ width: rightWidth + '%' }">右侧内容</div>
</div>
<button @click="adjustSplit(70)">调整比例为 70:30</button>
</template>
<script>
export default {
data() {
return {
leftWidth: 50,
rightWidth: 50,
};
},
methods: {
adjustSplit(leftPercent) {
this.leftWidth = leftPercent;
this.rightWidth = 100 - leftPercent;
},
},
};
</script>
<style>
.split-screen {
display: flex;
height: 100vh;
}
.left-pane, .right-pane {
height: 100%;
overflow: auto;
}
</style>
响应式分屏
通过 Vue 的响应式特性,结合媒体查询,可以实现不同屏幕尺寸下的分屏布局。
<template>
<div class="split-screen">
<div class="left-pane">左侧内容</div>
<div class="right-pane">右侧内容</div>
</div>
</template>
<style>
.split-screen {
display: flex;
height: 100vh;
}
.left-pane, .right-pane {
flex: 1;
height: 100%;
}
@media (max-width: 768px) {
.split-screen {
flex-direction: column;
}
}
</style>
以上方法可以根据实际需求选择使用,实现 Vue 中的分屏效果。






