I was getting really excited after reading this article about faster scroll effects. I went implementing Warry's method to see if I could spot any difference in performance - compared to using the scroll event listener:
window.addEventListener('scroll', function() {
console.log('Scrolling: ' + window.pageYOffset);
});
Code from article:
function loop() {
if (lastPosition == window.pageYOffset) {
requestAnimationFrame(loop);
return false;
} else lastPosition = window.pageYOffset;
console.log('Scrolling: ' + window.pageYOffset);
// make sticky calculations...
requestAnimationFrame(loop);
}
loop(); // start loop
These two pieces of code output the same (Y)offset and seem to be equivalent performance-wise. So my question is: Is the article right?
Also, how does requestAnimationFrame
fits in this picture? I know that it tells the browser that it wishes to perform a re-paint. Or... is position fixed simply the way to go? I'm interested in this topic because I'm working on a plugin for sticky elements.
Any info or advice is appreciated!