最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Difference between scroll and scrollTop in javascriptjQuery - Stack Overflow

programmeradmin2浏览0评论

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 to window.scrollTo, which is essentially the same as window.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
Add a ment  | 

3 Answers 3

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.

发布评论

评论列表(0)

  1. 暂无评论