scrollToTop() {
// 获取视窗高度
const domHight = document.body.offsetHeight
// dom滚动位置
const scrollTop =
window.pageYOffset ||
document.documentElement.scrollTop ||
document.body.scrollTop
// 获取监听元素
let id: any
// 获取监听元素本身高度
let scrollHeight
// 获取监听元素距离视窗顶部距离
let offsetTop
// 获取监听元素距离顶部高度-窗口高度
let top
// 元素距离底部的高度+元素本身高度
let bottom
// this.list.map( (i) => {
id = document.getElementById(`top1`)
scrollHeight = id.scrollHeight
offsetTop = id.offsetTop
top = offsetTop - domHight > 0 ? offsetTop - domHight : 0
bottom = scrollHeight + offsetTop
// 页面滚动位置 > 元素距离顶部高度-窗口高度 && 页面滚动位置 < 元素距离顶部高度+元素本身高度
if (scrollTop >= top && scrollTop <= bottom) {
this.top1type = true
}
// });
}
mounted() {
window.addEventListener('scroll', this.scrollToTop)
}
addEventListener() 方法用于向指定元素添加事件