要做到这一点,我们需要一个 debounce(防抖动)方法,这个方法会将我们的取值请求加入一个队列中,在浏览器准备好绘制下一帧之前都不会重新取值,此时它已经计算出了页面上所有元素的位置,所以它不会不断重复相同的工作。
// 防抖动函数接受一个我们自定义的函数作为参数
const debounce = (fn) => { // 这包含了对 requestAnimationFrame 的引用,所以我们可以在我们希望的任何时候停止它
let frame;
// 防抖动函数将返回一个可以接受多个参数的新函数
return (...params) => {
// 如果 frame 的值存在,那就清除对应的回调
if (frame) {
cancelAnimationFrame(frame);
}
// 使我们的回调在浏览器下一帧刷新时执行
frame = requestAnimationFrame(() => {
// 执行我们的自定义函数并传递我们的参数
fn(...params);
});
}
};
// Reads out the scroll position and stores it in the data attribute
// so we can use it in our stylesheets
const storeScroll = () => {
document.documentElement.dataset.scroll = window.scrollY;
}
// Listen for new scroll events, here we debounce our `storeScroll` function
document.addEventListener('scroll', debounce(storeScroll));
// Update scroll position for first time
storeScroll();
通过标记事件为 passive 状态,我们可以告诉浏览器我们的滚动事件不会被触摸交互阻止(例如与谷歌地图等插件交互时)。这允许浏览器立即滚动页面,因为它现在知道该事件不会被阻止。
document.addEventListener('scroll', debounce(storeScroll), { passive: true });解决了性能问题后,我们现在可以通过稳定的方式使用 JavaScript 将获取的数据提供给 CSS,并可以使用它来为页面上的元素添加样式。
Live Demo on CodePen










