Tuesday, May 21, 2024
 Popular · Latest · Hot · Upcoming
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

 Answers
143

scrollBy will scroll from actual position to number of pixel defined I think you might take a look at scrollTo


[#80773] Thursday, January 17, 2013, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
dominickmackenziet

Total Points: 583
Total Questions: 101
Total Answers: 117

Location: Saint Lucia
Member since Wed, Feb 8, 2023
1 Year ago
dominickmackenziet questions
Wed, Apr 7, 21, 00:00, 3 Years ago
Fri, Feb 12, 21, 00:00, 3 Years ago
;