Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you use jQuery measure how far down the user has scrolled?

Tags:

jquery

css

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?

like image 294
JD Graffam Avatar asked Aug 15 '09 22:08

JD Graffam


1 Answers

See scrollTop, scrollLeft and Events/Scroll.

Example:

$('div#something').scroll(function () {
    if ($(this).scrollTop() > 200) {
        $(this).addClass('foo');
    } else {
        $(this).removeClass('foo');
    }
});
like image 107
karim79 Avatar answered Sep 22 '22 02:09

karim79