思路:
监听鼠标按下事件,监听在移动时计算它的高度和上面div盒子的高度
demo
<template>
<div style="width: 100%;">
<div
ref="DrawerBody"
class="drawer-body"
>
<div>11</div>
<div>11</div>
<div>11</div>
<div>11</div>
<div>11</div>
<div>11</div>
<div>11</div>
<div>11</div>
<div>11</div>
<div>11</div>
<div>11</div>
<div>11</div>
<div>11</div>
</div>
<div
ref="DrawerBottom"
class="drawer-bottom"
>
<div
ref="DrawerBottomBar"
class="drawer-bottom-bar"
>
<div class="drawer-bottom-bar-true" />
</div>
<div class="drawer-bottom-body">
1111
</div>
</div>
</div>
</template>
<script lang="ts" setup>
import { onMounted, ref } from 'vue'
const DrawerBottomBar = ref<any>(null)
const DrawerBottom = ref<any>(null)
const DrawerBody = ref<any>(null)
onMounted(() => {
DrawerBottomBar.value.onmousedown = (e: any) => {
const oldHeight = DrawerBottom.value.offsetHeight
const startY = e.clientY
document.onmousemove = (e) => {
console.log(e)
// 当前鼠标Y值
const endY = e.clientY
// 当前鼠标在Y轴移动的距离
const distance = endY - startY
// left-top当前高度
const newHeight = oldHeight - distance
// 设置dom样式
DrawerBottom.value.style.height = `${newHeight}px`
DrawerBody.value.style['padding-bottom'] = `${newHeight}px`
}
document.onmouseup = () => {
document.onmousemove = null
document.onmouseup = null
}
return false
}
})
</script>
<style scoped lang="scss">
.drawer {
position: relative;
&-body {
background: #fafafa;
padding: 0 24px;
width: 100%;
font-family: "Inter";
height: calc(100vh - 86px);
overflow: auto;
&::-webkit-scrollbar {
display: none; /* Chrome Safari */
}
}
&-bottom {
width: 100%;
position: absolute;
z-index: 10;
bottom: 0;
background: #fff;
height: 360px;
border-top: 1px solid #e5e6eb;
box-shadow: 0px -4px 12px rgba(0, 0, 0, 0.12);
&-bar {
&-true {
height: 4px;
border-radius: 4px;
background: rgba(0, 0, 0, 0.32);
}
cursor: ns-resize;
position: absolute;
left: 50%;
margin-left: -20%;
top: -20px;
width: 40%;
padding: 10px 0;
}
}
}
</style>