Detect when scrolling has finished when using `scroll-behavior: smooth`

前端 未结 3 815
小鲜肉
小鲜肉 2021-02-18 14:19

What is the best way to detect when scrolling to an element on the page has finished? The spec says that \"The scrolling box is scrolled in a smooth fashion using a user-agent-d

3条回答
  •  梦毁少年i
    2021-02-18 14:56

    The only feasible option appears to be to just wait until there aren't any more scroll events:

    let timer;
    window.addEventListener( 'scroll', () => {
        clearTimeout( timer );
    
        timer = setTimeout( () => {
            callback();
        }, 300 );
    }, { passive: true } );
    

提交回复
热议问题