标签:监听 https ted fse listener detail sea off scroll
1、监听滚动事件
利用VUE写一个在控制台打印当前的scrollTop,
首先,在mounted钩子中给window添加一个滚动滚动监听事件,
mounted () { window.addEventListener(‘scroll‘, this.handleScroll) },
然后在方法中,添加这个handleScroll方法
handleScroll () { var scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop console.log(scrollTop) },
2、监听元素到顶部的距离 并判断滚动的距离是否大于元素到顶部的距离
handleScroll () { var scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop var offsetTop = document.querySelector(‘#searchBar‘).offsetTop if (scrollTop > offsetTop) { ... } else { ... } },
转载原文:https://blog.csdn.net/wang1006008051/article/details/78003974
标签:监听 https ted fse listener detail sea off scroll
原文地址:https://www.cnblogs.com/gaocd/p/14071880.html