I have code like below:
blah blah
blah blah 2&
-
If your elements aren't the same height, you can iterate over them on scroll:
$(document).scroll(function() {
var cutoff = $(window).scrollTop();
$('.item').removeClass('top').each(function() {
if ($(this).offset().top > cutoff) {
$(this).addClass('top');
return false; // stops the iteration after the first one on screen
}
});
});
If this is too slow, you can cache the $('.item').offset() into an array, rather than calling offset() each time.