143
rated 0 times
[
149]
[
6]
/ answers: 1 / hits: 22047
/ 12 Years ago, fri, january 18, 2013, 12:00:00
I'm trying to imitate scrollTop
(jQuery) in vanilla JS, so on click it scrolls to an element. This works fine - unless you have already scrolled past the element. So it doesn't scroll the opposite way. Should my formula incorporate window.pageYOffset
?
var moves = function(scrollz) {
var scrollPos = document.getElementById(scrollz).offsetTop - ((document.documentElement.clientHeight - document.getElementById(scrollz).offsetHeight) / 2);
var timerID = setInterval(function() {
window.scrollBy(0, speed);
if (window.pageYOffset >= scrollPos) {
clearInterval(timerID);
}
}, 13);
}
More From » scroll