Nosrettap
Nosrettap

Reputation: 11320

Difference between scroll and scrollTop in javascript/jQuery

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?

Upvotes: 7

Views: 8329

Answers (3)

Jyoti Duhan
Jyoti Duhan

Reputation: 1094

.scrollTo() or .scroll() is applicable on window object while .scrollTop() works with any DOM element

Upvotes: 3

leo
leo

Reputation: 8520

scrollTop uses window.scrollTo, it seems: http://james.padolsey.com/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.com/js-vs-jquery-scroll

There does not seem to be any differences in performance between window.scroll and window.scrollTo.

Upvotes: 1

Rumen Jekov
Rumen Jekov

Reputation: 1675

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.

Upvotes: -1

Related Questions