-
-
Save yoelfme/2df20e25208ea3f075a2 to your computer and use it in GitHub Desktop.
It's a function used to emulate to scrollTo of library jQuery but not using jQuery, only use pure Javascript :).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function goTo(container, idElementTo, duration,last_difference){ | |
if (duration < 0) return; | |
last_difference = typeof last_difference !== 'undefined' ? last_difference : -1; | |
to = document.getElementById(idElementTo); | |
var difference = to.offsetTop - container.scrollTop; | |
var perTick = difference / duration * 10; | |
if (last_difference === difference ) return; | |
setTimeout(function() { | |
container.scrollTop = container.scrollTop + perTick; | |
if (container.scrollTop === to.offsetTop) return; | |
goTo(container, to.id, duration - 10,difference); | |
}, 10); | |
}; | |
/* container: Is the element containing the element will to go, | |
idElementTo: Is the id of element will to go, | |
duration: Is the time at milliseconds for the transition, | |
last_difference: Obvious, | |
And then it would be used so: goTo(document.body,'myIdElement',500) | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment