当滚动到页面上的元素已完成时,检测的最佳方法是什么?规范说“滚动框在用户代理定义的时间段内使用用户代理定义的计时函数以平滑的方式滚动。用户代理应遵循平台约定,如果有的话。”
(2017-11-23) 在相关说明中,Firefox 58 now has a chrome-only scrollend
event.
最佳答案
唯一可行的选择似乎是等待,直到没有更多的 scroll
事件:
let timer;
window.addEventListener( 'scroll', () => {
clearTimeout( timer );
timer = setTimeout( () => {
callback();
}, 300 );
}, { passive: true } );
更新:Firefox 109 和 Chrome 110 现在有一个 scrollend 事件:https://caniuse.com/mdn-api_element_scrollend_event
关于javascript - 使用 `scroll-behavior: smooth` 检测滚动何时完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46827193/