jQuery: Scroll the window then addClass() - how to callback
Posted
by carillonator
on Stack Overflow
See other posts from Stack Overflow
or by carillonator
Published on 2010-05-06T18:33:15Z
Indexed on
2010/05/06
18:38 UTC
Read the original article
Hit count: 162
Inside a jQuery event handler, I would like to scroll the window and then add a class to something. This is my code:
$('#foo').click(function() {
window.scrollTo(y);
$('#bar').addClass('active');
});
$(window).scroll(function() {
$('#bar').removeClass('active');
});
Notice I have another handler to remove that same class whenever the window is scrolled. The scrolling part works fine, but seems to run asynchronously, so removeClass()
happens after addClass()
but before the scrolling is finished. I don't know how to do this in plain javascript. I know there is a jQuery scrollTop()
function that does the same thing (but seems to have cross-browser issues), but it doesn't accept a callback.
What I really need is a callback to add the class after the scrolling is finished. thanks!
© Stack Overflow or respective owner