1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
let that = this
// 设置要监听的元素
let dom = document.querySelector(".load-text")
// 创建监听实例
let observe = new IntersectionObserver(function (muattion) {
// 判断元素是否可见
let status = muattion[0].isIntersecting
if (status && that.count < 50) {
setTimeout(() => {
that.count += 10
}, 500)
}
})
// 开始观察
observe.observe(dom)

上面的代码首先观察类名是 load-text 的元素,如果该元素可以被看到,则 status 就是true,我们可以利用此特性来做数据懒加载或者触底函数。效果和监听
scroll 滚动一致,但是代码更简单。