api.jquery.com icon indicating copy to clipboard operation
api.jquery.com copied to clipboard

Document how to use scrollTop() properly

Open jzaefferer opened this issue 11 years ago • 18 comments

Via discussion in #14515.

This should be added to scrollTop():

Note that scrollTop can be used to read, but not write, the document scroll position. To scroll the document, animated or not, you have to call `scrollTop()` on the body element:

<pre><code>
// Without animation
$( document.body ).scrollTop( 500 );

// With animation
$( document.body ).animate({
  scrollTop: 500
});
</code></pre>

Any examples elsewhere that suggest $( "html, body" ).animate({ scrollTop: ... }); are wrong, since this will cause the animation callback to get triggered twice.

jzaefferer avatar Jan 13 '14 11:01 jzaefferer

I just reopened the Core ticket, see there for details: http://bugs.jquery.com/ticket/14515#comment:8

jzaefferer avatar Jan 28 '14 10:01 jzaefferer

I left some comments in #425 about the message here, I think we need to re-word it a bit and also add a similar note to scrollLeft()

gnarf avatar Jan 28 '14 17:01 gnarf

@jzaefferer I left a comment on the Core ticket for you.

kswedberg avatar Jan 28 '14 18:01 kswedberg

Since Core won't try to normalize this, all we can do is document the canonical solution. Afaik this snippet works consistently and produces a single callback:

$( "html, body" ).animate({
  // scroll to end of page
  scrollTop: $(document).height()
}, 800).promise().then(function() {
  console.log("runs once!")
});

@leobalter would you update your branch and create a new PR? Thanks.

jzaefferer avatar Mar 03 '14 22:03 jzaefferer

That snippet has other side effects...

Namely:

$( "html, body" ).animate({
  // scroll to end of page
  scrollTop: $(document).height()
}, 800).promise().then(function() {
  console.log("runs once!")
}).animate({
  scrollTop: 0
}, 800); // but only after this animation ALSO finishes

$("body").animate({opacity: 0}, 500); // oh this one too

The promise on a jQuery object only gets called when its default animation queue empties, not as an inline callback to animation completion.

gnarf avatar Mar 03 '14 22:03 gnarf

As gnarf said, you probably want something akin to this:

$.Deferred(function( defer ) {
    $( "html, body" ).animate({
        // scroll to end of page
        scrollTop: $(document).height()
    }, 800, defer.resolve );
}).done(function() {
    console.log( "runs once!" )
});

jaubourg avatar Mar 04 '14 18:03 jaubourg

IMHO, some of these examples are looking way complex to be presented as examples.

I think we can use the suggestions made by @gnarf to mention scrollLeft and we can also add another example using $.Deferred as mentioned by @jaubourg.

It might be worth to mention that jQuery Core don't try to force a normalization on this and explain why it's needed to handle scrollTop both on html and body to get a crossbrowser solution.

leobalter avatar Mar 05 '14 04:03 leobalter

We can start with an example that doesn't have the callback, to keep things simple at first. Use that to talk about the underlying issue and lack of normalization. Then talk about the issue with animation callbacks and how to deal with that, along with another example.

jzaefferer avatar Mar 07 '14 10:03 jzaefferer

Can this be an article on learn perhaps, with a link from the api docs? It's starting to sound pretty big.

dmethvin avatar Mar 07 '14 14:03 dmethvin

:+1: to linking to a learn article.

kswedberg avatar Mar 07 '14 14:03 kswedberg

I'll try to write something by the weekend.

challenge

Anyway, I don't want to prevent anyone else to write it as well. If we have other article to be posted on learn I can push mine to a blog, maybe.

leobalter avatar Mar 07 '14 16:03 leobalter

I'll try to write something by the weekend.

@leobalter Still interested in taking this? Or did you already do?

arthurvr avatar Feb 08 '15 16:02 arthurvr

I failed doing this. I may try it again but it shouldn't hold anyone else to fix it.

On Feb 8, 2015, at 11:23 AM, Arthur Verschaeve [email protected] wrote:

I'll try to write something by the weekend.

@leobalter Still interested in taking this? Or did you already do?

— Reply to this email directly or view it on GitHub.

leobalter avatar Feb 08 '15 16:02 leobalter

@arthurvr interested in taking this over? Or finding someone to write the docs? Sad to see this still open more than a year later...

jzaefferer avatar Apr 14 '15 17:04 jzaefferer

interested in taking this over? Or finding someone to write the docs? Sad to see this still open more than a year later...

That's true for basically ever ticket open here :( One day I'll get to it for sure, but time just limits me.

arthurvr avatar Apr 14 '15 17:04 arthurvr

@jzaefferer Although I do definitely agree that many things in this repo are taking way too long. There are still APIs from back in 2012 undocumented, just because we lack the time and the resources.

arthurvr avatar Apr 14 '15 17:04 arthurvr

@arthurvr the important thing is that we are making progress, thanks to you! This issue in particular seemed to get lots of people suggesting changes without a clear consensus on what to do. I think that's why it sat around so long, it is tedious to sort through these kinds of tickets and there is always concern you'll upset someone by ignoring their concerns. So go for the low-hanging fruit if it's easier.

dmethvin avatar Apr 15 '15 01:04 dmethvin

Here's a good explanation of the underlying mess, along with a future-proof solution called document.scrollingElement: https://dev.opera.com/articles/fixing-the-scrolltop-bug/

If I read that correctly, we should document how to animate document.scrollingElement, along with a recommendation to load the polyfill.

jzaefferer avatar May 20 '15 15:05 jzaefferer