如何在Chrome中模拟CSS Scroll Snap Points?

Mar*_*cka 15 css polyfills

Firefox 39,Safari 9和IE11支持CSS Scroll Snap Points.Chrome具有开发功能.

是否存在可以模拟以下CSS样式的polyfill:

-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;
Run Code Online (Sandbox Code Playgroud)

直到该功能由Chrome实施?

Luc*_*sco 5

如果您愿意考虑使用一致的跨浏览器行为重新实现此功能,请使用 此库

为什么

使用此方法而不是本机css解决方案的主要原因是,它可在所有现代浏览器中使用,并具有可自定义的配置,以允许在过渡和滚动检测中进行自定义计时。

怎么样

该库使用香草javascript缓动函数重新实现了CSS捕捉功能,并使用了容器元素的scrollTop/ scrollLeft属性值和滚动事件监听器来工作

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();
Run Code Online (Sandbox Code Playgroud)


Mar*_*cka 0

Chrome 69 将包含该功能。无需解决方法。


只需坐下来等待即可。坐下来等待:-)