What is the difference between:
window.scroll(0,200);
AND
$(window).scrollTop(200);
Apart from the fact that one of them is using jQuery and the other is not, what is the difference? Does one animate the scroll and the other doesn't? Will one work faster than the other?
What is the difference between:
window.scroll(0,200);
AND
$(window).scrollTop(200);
Apart from the fact that one of them is using jQuery and the other is not, what is the difference? Does one animate the scroll and the other doesn't? Will one work faster than the other?
Share Improve this question asked Jul 3, 2014 at 19:07 NosrettapNosrettap 11.3k25 gold badges88 silver badges146 bronze badges 3-
4
Judging from this line,
$.fn.scrollTop
essentially maps towindow.scrollTo
, which is essentially the same aswindow.scroll
. Does that answer your question? ) – raina77ow Commented Jul 3, 2014 at 19:14 -
As for performance, the usual considerations apply here -
window.scroll
is obviously faster (as the other method will invoke it after the several checks and function calls and object creations), but the difference will be negligible. – raina77ow Commented Jul 3, 2014 at 19:16 - @raina77ow sorry, didn't mean to steal your answer, I didn't see your ments before answering – leo Commented Jul 3, 2014 at 19:22
3 Answers
Reset to default 3.scrollTo() or .scroll() is applicable on window object while .scrollTop() works with any DOM element
scrollTop
uses window.scrollTo
, it seems: http://james.padolsey./jquery/#v=1.10.2&fn=jQuery.fn.scrollTop
Performance wise the pure js solution is faster, obviously, but in most cases it shouldn't really matter: http://jsperf./js-vs-jquery-scroll
There does not seem to be any differences in performance between window.scroll
and window.scrollTo
.
The jQuery scrollTop() method - Returns the vertical scrollbar position for an HTML element The JavaScript window.scroll(x-coord,y-coord) method scrolls the window to a particular place in the document. The window.scrollTo is effectively the same as this method.