He does not wait
It animates your document from top to top ... Funny, but true. You have other alternatives. Or:
- check the position of your page before scrolling (as suggested by another by providing some code) or
- use some jQuery plugin that works differently.
My .scrollintoview()
The jQuery plugin works just like that. It scrolls only if necessary, and starts the full handler after the scroll is completed (if there was a scroll in the first place) or immediately if the scroll is not needed.
But for this you need an element that needs to be scrolled. You are not actually scrolling any element, but just HTML. This, of course, is not the safest way. Better to use either:
$("html,body")
or
$(window)
More cross browser supported. Thus, your $("html")
may not work in all of them.
source share