作者:浪迹天涯沃热尔_441 | 来源:互联网 | 2023-01-12 17:14
我使用的是Javascript方法Element.scrollIntoView()
https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView
滚动结束时有什么办法可以让我知道.说有动画,或者我已经设定了{behavior: smooth}
.
我假设滚动是异步的,并想知道是否有任何回调像它的机制.
1> 小智..:
您可以使用IntersectionObserver
,检查是否元素.isIntersecting
在IntersectionObserver
回调函数
const element = document.getElementById("box");
const intersectiOnObserver= new IntersectionObserver((entries) => {
let [entry] = entries;
if (entry.isIntersecting) {
setTimeout(() => alert(`${entry.target.id} is visible`), 100)
}
});
// start observing
intersectionObserver.observe(box);
element.scrollIntoView({behavior: "smooth"});
body {
height: calc(100vh * 2);
}
#box {
position: relative;
top:500px;
}
box
2> niutech..:
没有scrollEnd
事件,但是您可以侦听scroll
事件并检查事件是否仍在滚动窗口:
var scrollTimeout;
addEventListener('scroll', function(e) {
clearTimeout(scrollTimeout);
scrollTimeout = setTimeout(function() {
console.log('Scroll ended');
}, 100);
});