Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

animate scrollTop fires scroll event

I have some code that listens to the scrolling of a page and it's responsible for two things, performing the necessary action (which works) when the "page" has changed but also animate the scrollTop to the nearest "page" but in doing so the scroll event is fired so it sequentially crawls through all the "pages" until it reaches the end of the document.

How can I stop animate scrollTop from firing the scroll event? Is it even possible?

"pages" because this is an iPad html page and each 1024x768 view is a "page"

like image 369
Dave Mackintosh Avatar asked Jun 26 '26 12:06

Dave Mackintosh


1 Answers

You can find a answer at this post : https://stackoverflow.com/a/1659231/237838

You could make write your own code to set the animation value, and set a flag indicating that the change comes from an animation.

For example: (Untested)

var scrollAnimating = false
jQuery.fx.step.scrollTop = function(E) {
    scrollAnimating = true;
    E.elem.scrollTop = E.now;
    scrollAnimating = false;
};

$('#gototop').click(function() {
    $('body').animate({scrollTop:0},3000);
    $(window).scroll(function () {
        if (!scrollAnimating)
            $('body').stop();
    });
    return false;
})
like image 126
Francois Borgies Avatar answered Jun 29 '26 02:06

Francois Borgies