Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to emulate CSS Scroll Snap Points in Chrome?

Tags:

css

polyfills

Firefox 39, Safari 9 and IE11 provide support for CSS Scroll Snap Points. Chrome has the feature in development.

Is there a polyfill that would emulate the following CSS styles:

-webkit-scroll-snap-type: mandatory;
-ms-scroll-snap-type: mandatory;
scroll-snap-type: mandatory;
-webkit-scroll-behavior: smooth;
-ms-scroll-behavior: smooth;
scroll-behavior: smooth;
-webkit-scroll-snap-points-y: repeat(600px);
-ms-scroll-snap-points-y: snapInterval(0px, 600px); /* Old syntax */
scroll-snap-points-y: repeat(600px);
overflow-y: auto;
overflow-x: hidden;

until the feature is implemented by Chrome?

like image 875
Martin Vseticka Avatar asked Jul 06 '15 11:07

Martin Vseticka


People also ask

Can I use CSS scroll snap?

CSS Scroll Snap is supported in all major browsers. A previous version of the specification was implemented in some browsers, and may appear in tutorials and articles. If material includes the deprecated scroll-snap-points-x and scroll-snap-points-y properties, it should be considered outdated.

What is CSS scroll snap?

CSS Scroll Snap is a module of CSS that introduces scroll snap positions, which enforce the scroll positions that a scroll container's scrollport may end at after a scrolling operation has completed.

What is scroll padding?

scroll-padding is part of the CSS Scroll Snap Module. Scroll snapping refers to “locking” the position of the viewport to specific elements on the page as the window (or a scrollable container) is scrolled.


2 Answers

If you're willing to consider a vanilla javascript re-implementation of this feature with a consistent cross browser behaviour you can use this library

Why

The main reason to use this instead of the native css solution is that it works in all modern browsers and has a customizable configuration to allow custom timing in transitions and scrolling detection.

How

The library re-implements the css snapping feature using vanilla javascript easing functions, and works using the values of the container element's scrollTop/scrollLeft properties and the scroll Event Listener

Example

import createScrollSnap from 'scroll-snap'

const element = document.getElementById('container')

const { bind, unbind } = createScrollSnap(element, {
  snapDestinationX: '0%',
  snapDestinationY: '90%',
  timeout: 100,
  duration: 300,
  threshold: 0.2,
  snapStop: false,
  easing: easeInOutQuad,
}, () => console.log('snapped'))

// remove the listener 
// unbind();

// re-instantiate the listener 
// bind();
like image 192
Luca Falasco Avatar answered Oct 04 '22 17:10

Luca Falasco


I found a polyfill: https://github.com/zigotica/scrollSnapPointsPolyfill

Haven't tested it extensively.

like image 39
ngstschr Avatar answered Oct 04 '22 16:10

ngstschr