Skip to content

Instantly share code, notes, and snippets.

@w-jerome
Last active March 27, 2019 08:42
Show Gist options
  • Save w-jerome/0c4018590a7a2383d3df4582dd4857d2 to your computer and use it in GitHub Desktop.
Save w-jerome/0c4018590a7a2383d3df4582dd4857d2 to your computer and use it in GitHub Desktop.
Javascript — Scroll To
// vanillaJS : window.scrollTo(0,0)
var $links = document.querySelectorAll('a[href*="#"]');
for (var i = 0; i < $links.length; i++) {
$links[i].onclick = function() {
var selector = this.href.split('#')[1];
var $target = document.querySelector('#'+selector);
var scrollTo = ($target) ? $target.getBoundingClientRect().top + document.documentElement.scrollTop : 0;
if (!$target) {
return false;
}
TweenLite.to($html, 0.75, {
scrollTop: scrollTo,
ease: Power2.easeOut,
});
return false;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment