项目背景
背景跟 用position:fixed做粘性定位 有关。顶部元素用 fixed 做了粘性定位后,希望滚动到该定位元素的下方。
代码:
<div>
<div class="lives-padding"></div>
<div class="lives"></div>
<table>xxx</table>
...
<div @click="handleScroll"></div>
<div ref="shop">跳到lives下面</div>
<div>
首先获取 lives 的高度,再获取 shop 距离顶部的距离。
handleScroll() {
const offsetHeight = document.getElementsByClassName('lives')[0].offsetHeight
const top = this.$refs.shop.offsetTop
window.scrollTo({
top: top - offsetHeight,
left: 0,
behavior: 'smooth',
})
}
使用 window.scrollTo 滚动。