Maybe it's a stupid question but i want to know why this is not working.
I try to scroll to this element with animation. Normally the element is:
scrollTo(document.body, 0, 100);
I changed the element in a variable named 'here'. If I think logically, the code must scroll to element 'here', to 0px, duration, but i doesn't work. Is there anybody who can tell me whats going wrong?
My code:
document.getElementById('scroll-to-elm').onclick = function () {
var here = document.getElementById('tohere');
scrollTo(here, 0, 100);
}
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);
}