You can use CSS Scroll Snap .
However, this feature is now deprecated, so if you want to consider a cross-browser javascript with a cross-browser using the built-in CSS Scroll Snap specification, as already answered here: How to emulate CSS scrollbar anchors in Chrome? , you can use this library . I wrote.
The main reason for using this instead of your own css solution is that it works in all modern browsers and has a configurable configuration that allows you to configure synchronization when navigating and scrolling.
The library reimplementes the css binding function using javascript easing functions in vanilla and works using the property values ββof the scrollTop / scrollLeft container element and the scroll of the Event Listener
Here is an example that shows how to use it:
import ScrollSnap from 'scroll-snap' const snapConfig = { scrollSnapDestination: '90% 0%', // scroll-snap-destination css property, as defined here: https://developer.mozilla.org/en-US/docs/Web/CSS/scroll-snap-destination scrollTimeout: 100, // time in ms after which scrolling is considered finished scrollTime: 300 // time in ms for the smooth snap } function callback () { console.log('called when snap animation ends') } const element = document.getElementById('container') const snapObject = new ScrollSnap(element, snapConfig) snapObject.bind(callback) // unbind the element // snapObject.unbind();
You can see a working demo here.
Luca Falasco
source share