Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

document.body.scrollTop Firefox returns 0 : ONLY JS

Any alternatives in pure javascript?

The following works in opera, chrome and safari. Have not tested yet on explorer:

http://monkey-me.herokuapp.com

https://github.com/coolcatDev/monkey-me-heroku/blob/master/static/js/myscripts.js

At page load should scroll down to div '.content':

var destiny = document.getElementsByClassName('content'); var destinyY = destiny[0].offsetTop; scrollTo(document.body, destinyY, 200);  function scrollTo(element, to, duration) {     if (duration <= 0) return;     var difference = to - element.scrollTop;     var perTick = difference / duration * 2;      setTimeout(function() {         element.scrollTop = element.scrollTop + perTick;         scrollTo(element, to, duration - 2);     }, 10); }; 
like image 722
Al Ex Tsm Avatar asked Feb 20 '15 16:02

Al Ex Tsm


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 .

Can I use document documentElement scrollTop?

Generally you can use (document. documentElement. scrollTop || document. body.


1 Answers

Try using this: document.documentElement.scrollTop. If I am correct document.body.scrollTop is deprecated.

Update

Seems like Chrome does not play along with the answer, to be safe use as suggested by @Nikolai Mavrenkov in the comments:

window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0

Now all browsers should be covered.

like image 61
Michelangelo Avatar answered Sep 19 '22 05:09

Michelangelo