如何知道滚动到元素是在 Javascript 中完成的? [英] How to know scroll to element is done in Javascript?
问题描述
我正在使用 Javascript 方法 Element.scrollIntoView()
https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView
I am using Javascript method Element.scrollIntoView()
https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView
有什么方法可以让我知道滚动何时结束.假设有一个动画,或者我已经设置了 {behavior: smooth}
.
Is there any way I can get to know when the scroll is over. Say there was an animation, or I have set {behavior: smooth}
.
我假设滚动是异步的,想知道是否有任何类似机制的回调.
I am assuming scrolling is async and want to know if there is any callback like mechanism to it.
推荐答案
没有 scrollEnd
事件,但是你可以监听 scroll
事件并检查它是否存在仍在滚动窗口:
There is no scrollEnd
event, but you can listen for the scroll
event and check if it is still scrolling the window:
var scrollTimeout;
addEventListener('scroll', function(e) {
clearTimeout(scrollTimeout);
scrollTimeout = setTimeout(function() {
console.log('Scroll ended');
}, 100);
});
这篇关于如何知道滚动到元素是在 Javascript 中完成的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!