Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When user tries to scroll mid-animation, flickering occurs as scrollTo fights the scrollbar #53

Closed
gregplaysguitar opened this issue Sep 25, 2013 · 8 comments
Assignees

Comments

@gregplaysguitar
Copy link

There are two possible solutions - either 1) ignore user scrolling until the animation is complete, or 2) cancel the animation so the user regains control. I'm not sure how easy 1) would be to implement, but 2) is very simple, as the following function demonstrates:

function polite_scroll_to(val, duration, callback) {
    /* scrolls body to a value, without fighting the user if they
       try to scroll in the middle of the animation. */

    var auto_scroll = false;

    function stop_scroll() {
        if (!auto_scroll) {
            $("html, body").stop(true, false);
        }
    };
    $(window).on('scroll', stop_scroll);

    $("html, body").animate({
        scrollTop: val
    }, {
        duration: duration,
        step: function() {
            auto_scroll = true;
            $(window).one('scroll', function() {
                auto_scroll = false;
            });
        },
        complete: function() {
            callback && callback();
        },
        always: function() {
            $(window).off('scroll', stop_scroll);
        }
    });

};
@flesler
Copy link
Owner

flesler commented Sep 25, 2013

It's indeed not complicated to implement, but my goal with scrollTo is remain as a wrapper to $.fn.animate(). That jQuery method lets you interrupt the animation but won't do it automatically.
So if you need this, you can add this from outside, there's no need to change the code inside the plugin. Something like this should be enough:

$(window).scroll(function() {
   $(window)._scrollable().stop(true, false);
});

@flesler flesler closed this as completed Sep 25, 2013
@gregplaysguitar
Copy link
Author

That snippet just renders scrollTo useless... have you actually tried it? The window.scroll event catches all scrolling, not just user-initiated.

Even if you're not willing to fix this, it's currently not possible to implement something like my function above with scrollTo, because it doesn't offer a step callback.

@flesler
Copy link
Owner

flesler commented Sep 25, 2013

What I'm saying is, it's not something you MUST include inside the plugin (which I consider a good thing).
Still, now giving this a second thought could just add it and make it optional by passing a boolean setting. It won't play too nicely with localScroll or serialScroll but as far a scrollTo as a standalone plugin, it does make sense indeed.

I'll think about this a bit, will probably land the change soon.
Thanks

@flesler flesler reopened this Sep 25, 2013
@gregplaysguitar
Copy link
Author

Great, thanks.

@laukstein
Copy link

This bug must be fixed, related to #8.

@flesler
Copy link
Owner

flesler commented Sep 26, 2013

Note that it's not a bug. It's the way it is intended to work. The same will happen with any jquery animation where the user gets to alter the same style the animation is modifying.
I will make it optional by adding a boolean setting. Will let you know when I land the change.

flesler added a commit that referenced this issue Oct 11, 2013
@ghost ghost assigned flesler Jan 13, 2014
@flesler
Copy link
Owner

flesler commented Mar 18, 2015

Hi, I will try the approach mentioned on #67.
This issue is older but I'll close it as dup and proceed on the other one.
Thanks

@flesler flesler closed this as completed Mar 18, 2015
@flesler
Copy link
Owner

flesler commented Mar 18, 2015

This is implemented. To cancel on user scroll pass {interrupt:true}, if you want a callback that is called when the scroll is interrupted, you can pass a fail callback that is provided natively by jQuery. You can check the demo I updated it with an example of both settings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants