Skip to content

Instantly share code, notes, and snippets.

@yoelfme
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yoelfme/2df20e25208ea3f075a2 to your computer and use it in GitHub Desktop.
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 :).
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