1、动态绑定样式
<div class="left" :style="{ top: topHeight + 'px' }"></div>
css样式:
.left {
width: 72px;
height: 402px;
background-color: #ffffff;
border-radius: 5px;
text-align: center;
display: flex;
flex-wrap: wrap;
justify-content: space-around;
padding-top: 10px;
position: fixed;
left: 12%;
// top: 38%;
}
2、监听滚动条高度
<script setup lang="ts">
// 默认侧边栏高度
const topHeight = ref(402);//侧边栏初始高度
onMounted(() => {
window.addEventListener("scroll", scrollTop, true);
});
const scrollTop = () => {
let scroll = document.documentElement.scrollTop || document.body.scrollTop;
console.log("网页正文全文高",document.body.scrollHeight);
topHeight.value = 402 - Math.floor(scroll / Math.floor(document.body.scrollHeight/173));
}
</script>