Is there a built in jQuery function to determine the length of a scroll?
I’m trying to write a function that will add a class to a div if the user has scrolled 50 pixels from the top.
So the code will look like this:
if(userHasScrolled) {
$('#myDiv').addClass('hasScrolled');
}
You can check
$(document).scrollTop()inside of a scroll handler:If adding the class name is all you want (no other actions needed), this could be shortened to:
See http://api.jquery.com/scrollTop/.
Note that it is very inefficient to use
scrollevent handlers.