可拖动的分隔条来调整两个 div 的宽度,js实现
2024-08-05 09:56:27 阅读次数:18
div,js
<template>
<div id="container" ref="container">
<div id="left-panel" :>
左侧面板
</div>
<div id="splitter" @mousedown="startDrag">
||
</div>
<div id="right-panel">
右侧面板
</div>
</div>
</template>
<script setup>
import { ref } from 'vue';
const container = ref(null);
const leftWidth = ref(200); // 设置左侧面板的初始宽度
function startDrag(event) {
const startX = event.clientX;
const startWidth = leftWidth.value;
function onMouseMove(e) {
const currentX = e.clientX;
leftWidth.value = startWidth + (currentX - startX);
// Optional: Limit the width so it doesn't go too small or too large
if (leftWidth.value < 100) leftWidth.value = 100;
if (leftWidth.value > container.value.offsetWidth - 100) {
leftWidth.value = container.value.offsetWidth - 100;
}
}
function onMouseUp() {
document.removeEventListener('mousemove', onMouseMove);
document.removeEventListener('mouseup', onMouseUp);
}
document.addEventListener('mousemove', onMouseMove);
document.addEventListener('mouseup', onMouseUp);
}
</script>
<style>
#container {
display: flex;
height: 100%;
}
#left-panel {
background-color: #ddd;
height: 100%;
overflow: auto;
}
#splitter {
background-color: #888;
width: 5px;
cursor: col-resize;
user-select: none; /* Prevent text selection while dragging */
}
#right-panel {
background-color: #bbb;
flex-grow: 1;
height: 100%;
overflow: auto;
}
</style>
版权声明:本文内容来自第三方投稿或授权转载,原文地址:https://blog.51cto.com/u_15939684/10041001,作者:actionLife,版权归原作者所有。本网站转在其作品的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如因作品内容、版权等问题需要同本网站联系,请发邮件至ctyunbbs@chinatelecom.cn沟通。
上一篇:error: command ‘x86_64-linux-gnu-gcc‘ failed with exit status 1
下一篇:读 Beginning Android Games 2nd Edition (十五) 游戏主菜单