I need to change the style of an element after the user has scrolled down beyond a certain number of pixels, and then change it back once the user has scrolled back up. I'm using jQuery already so I'd like to use jQuery if possible. Can anyone provide an example where you add a a classname to a div once the user has scrolled beyond 200 pixels and then remove the classname once the user has scrolled back up to less than 200 pixels?
See scrollTop
, scrollLeft
and Events/Scroll
.
Example:
$('div#something').scroll(function () {
if ($(this).scrollTop() > 200) {
$(this).addClass('foo');
} else {
$(this).removeClass('foo');
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With