Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
76
rated 0 times [  81] [ 5]  / answers: 1 / hits: 50405  / 9 Years ago, fri, february 20, 2015, 12:00:00

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);
};

More From » firefox

 Answers
29

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.


[#67737] Thursday, February 19, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
dillionsalvadorg

Total Points: 288
Total Questions: 103
Total Answers: 75

Location: South Korea
Member since Sat, Oct 2, 2021
3 Years ago
;