vue实现左右拖动分屏

效果图如下:
封装组件
html 复制代码
<template>
    <div ref="container" class="container">
        <div class="left-content" :style="leftStyle">
        	/**定义左侧插槽**/
            <slot name="left"></slot>
        </div>
        <div ref="spliter" style="height: 100%; width: 10px" class="spliter-bar" />
        <div class="right-content" :style="rightStyle">
        	/**定义右侧插槽**/
            <slot name="right"></slot>
        </div>
    </div>
</template>

<script setup lang="ts">
import { ref, onMounted, onUnmounted } from 'vue';

const container:any = ref(null);
const spliter:any = ref(null);
const leftStyle:any= ref({});
const rightStyle:any = ref({});
const ratio:any = ref(0.6);  //初始化左右的宽度


const emits = defineEmits(['changeIframe'])

function updatePaneStyles(newRatio:any) {
    leftStyle.value = { width: `calc(${newRatio * 100}% - 5px)` };
    rightStyle.value = { width: `calc(${(1 - newRatio) * 100}% - 5px)` };
}

function handleResize(e:any) {
    const containerWidth = container.value.clientWidth;
    const rect = container.value.getBoundingClientRect();
    const initX = rect.left;
    function onMouseMove(e:any) {
        emits('changeIframe','none')
        e.preventDefault();
        // 限制鼠标移动事件的范围为container容器四至范围内
        if (e.clientX < rect.left || e.clientX > rect.right || e.clientY < rect.top || e.clientY > rect.bottom) {
            onMouseUp();
        }
        const moveScale = (e.clientX - initX) / containerWidth;
        const newRatio = moveScale;
        if (newRatio > 0.05 && newRatio < 0.95) {
            ratio.value = newRatio;
            updatePaneStyles(newRatio);
        }
    }

    function onMouseUp() {
        emits('changeIframe','auto')
        document.removeEventListener('mousemove', onMouseMove);
        document.removeEventListener('mouseup', onMouseUp);
    }

    document.addEventListener('mousemove', onMouseMove);
    document.addEventListener('mouseup', onMouseUp);
}

function onDblClick(e:any) {
    ratio.value = 0.6;
    updatePaneStyles(ratio.value);
}

onMounted(() => {
    updatePaneStyles(ratio.value);
    if (spliter.value) {
        spliter.value.addEventListener('mousedown', handleResize, false);
        spliter.value.addEventListener('dblclick', onDblClick, false);
    }
})
onUnmounted(() => {
    if (spliter.value) {
        spliter.value.removeEventListener('mousedown', handleResize);
        spliter.value.removeEventListener('dblclick', onDblClick);
    }
})
</script>

<style scoped>
.container {
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: row;
}

.left-content {
    height: 100%;
    z-index: 1;
    overflow: scroll;
    display: flex;
    flex-direction: column;
}

.right-content {
    height: 100%;
    z-index: 1;
}

.spliter-bar {
    cursor: col-resize;
    position: relative;
    z-index: 2;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;

    &:before,
    &:after {
        content: '';
        position: absolute;
        top: 50%;
        left: 50%;
        background-color: rgba(0, 0, 0, 0.15);
        width: 1.5px;
        height: 30px;
    }

    &:before {
        margin-left: -2px;
    }

    &:after {
        margin-left: 1px;
    }

    &:hover:before,
    &:hover:after {
        width: 1.5px;
        background-color: rgba(0, 0, 0, 0.35);
    }
}
</style>
页面使用

注意⚠️:

如果内容区域里面有iframe标签引入的页面,此时左右拖动会出看卡顿显现,此时可能通过动态修改pointerEvents的值来解决卡顿问题。当鼠标按下拖动的时候,将pointerEvents设置为'none',鼠标弹起的时候设置为'auto',默认值是'atuo'(如果左右是正常布局没有iframe加载的内容则可以忽略上面的触发父组件的事件)



组件封装参考http://t.csdnimg.cn/u7RVl

相关推荐
花楸树19 分钟前
前端搭建 MCP Client(Web版)+ Server + Agent 实践
前端·人工智能
wuaro20 分钟前
RBAC权限控制具体实现
前端·javascript·vue
专业抄代码选手24 分钟前
【JS】instanceof 和 typeof 的使用
前端·javascript·面试
用户00798136209725 分钟前
6000 字+6 个案例:写给普通人的 MCP 入门指南
前端
用户876128290737429 分钟前
前端ai对话框架semi-design-vue
前端·人工智能
干就完了132 分钟前
项目中遇到浏览器跨域前端和后端解决方案以及大概过程
前端
我是福福大王34 分钟前
前后端SM2加密交互问题解析与解决方案
前端·后端
实习生小黄38 分钟前
echarts 实现环形渐变
前端·echarts
_未知_开摆1 小时前
uniapp APP端在线升级(简版)
开发语言·前端·javascript·vue.js·uni-app
喝拿铁写前端1 小时前
不同命名风格在 Vue 中后台项目中的使用分析
javascript·vue.js