JavaScript
There’s almost nothing going on here. Set an event listener on the document for scroll. In the caught event, find the scroll movement using scrollY. Add that to the existing frame number and add a large multiple of numberOfFrames to account for negative numbers (you don’t want a negative frame number). Then, find the modulus of that number to get a final frame number between zero and the maximum number of frames. Finally, set that number to the container’s data attribute, and let the CSS do the rendering work.
const frameContainer = document.getElementById('frame-container');
const numberOfFrames = 16 * 16;
let number = 0;
let lastKnownScrollPosition = 0;
function updateEarth(value) {
number = ((number + value) + (numberOfFrames * 100)) % numberOfFrames;
frameContainer.dataset.number = number;
}
document.addEventListener('scroll', (event) => {
const scrollPosition = Math.floor(window.scrollY);
const movementY = scrollPosition - lastKnownScrollPosition;
lastKnownScrollPosition = scrollPosition;
window.requestAnimationFrame(() => {
updateEarth(movementY);
});
});