Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

javascript

css

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-defined timing function over a user-agent-defined period of time. User agents should follow platform conventions, if any."


(2017-11-23) On a related note, Firefox 58 now has a chrome-only scrollend event.

like image 838
Nikolay Yordanov Avatar asked Oct 19 '17 09:10

Nikolay Yordanov


People also ask

How do I fix smooth scrolling?

Anyway, to disable smooth scrolling in Chrome, follow these steps: Step 1: Type in chrome://flags/#smooth-scrolling in Chrome's address bar and press Enter. Step 2: To turn the feature off, click on the disable link. Then, click on a restart button to complete the process.

What does scroll-behavior Smooth do?

Definition and Usage The scroll-behavior property specifies whether to smoothly animate the scroll position, instead of a straight jump, when the user clicks on a link within a scrollable box.

Is there a safari equivalent for scroll-behavior Smooth?

Safari 15.4 adds support for CSS scroll-behavior as detailed in the 15.4 release notes. Added support for the CSS scroll-behavior property and ScrollOptions, allowing smooth scrolling to anchors or via JavaScript. I've tested and it works on 15.4 (Safari Technology Preview).

How do I smooth a scroll to an element?

To scroll to an element, just set the y-position to element. offsetTop . The SmoothScroll.


1 Answers

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 } ); 
like image 165
Nikolay Yordanov Avatar answered Sep 30 '22 13:09

Nikolay Yordanov