Tying animation elements to the quantity of scroll events is going to give inconsistent results. On the sample page you linked, the animation is tied to both absolute scroll position and quantity of scroll events, which can give some strange results.
There's so many different ways to scroll a view, and all of them will produce a different numbers of scroll events and scroll distances.
- Mouse wheel (affected by system mouse settings, resolution of mouse)
- Keyboard up and down keys (affected by system key repeat rates)
- Page up and down (possibly affected by some other settings?)
- Dragging the scroll bar (affected by all sorts of things)
And then all of these may well behave differently based on the browser.
In short? Tying animation to the quantity of scroll events sounds like a bad idea to me. Instead, why not just tie it to the scroll position?
function animateHorse() {
var currentScrollPosition = window.pageYOffset;
var imageIndex = Math.round(currentScrollPosition / scrollResolution);
if (imageIndex >= pictureCount) {
imageIndex = pictureCount - 1; // Select last image
}
$("#container img").hide();
$("#container img").eq(imageIndex).show();
}
And here's the fiddle.