Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why to use 'html, body' for scrollTop instead of just 'html'

Tags:

I got a solution from stackoverflow to scroll to the top of the page like

$('html, body').animate({scrollTop: $("#page").offset().top}, 2000); 

But I am not sure why to use 'html, body' for scrollTop instead of just 'html' ?

like image 967
Hector Barbossa Avatar asked Aug 31 '12 21:08

Hector Barbossa


People also ask

Is scrollTop deprecated?

scrollTop is deprecated in strict mode.

What is Document body scrollTop?

scrollTop property gets or sets the number of pixels that an element's content is scrolled vertically. An element's scrollTop value is a measurement of the distance from the element's top to its topmost visible content. When an element's content does not generate a vertical scrollbar, then its scrollTop value is 0 .

What does scrollTop return?

.scrollTop()Returns: Number Description: Get the current vertical position of the scroll bar for the first element in the set of matched elements or set the vertical position of the scroll bar for every matched element.


1 Answers

Some browsers apply the "overall" scroll to document.documentElement (the <html> element) and others to document.body (the <body> element). For compatibility with both, you have to apply the scrolling to both.

like image 68
Niet the Dark Absol Avatar answered Oct 05 '22 02:10

Niet the Dark Absol