admin管理员组文章数量:1316974
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.
本文标签: Difference between scroll and scrollTop in javascriptjQueryStack Overflow
版权声明:本文标题:Difference between scroll and scrollTop in javascriptjQuery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742021310a2414726.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论