Skip to content

Instantly share code, notes, and snippets.

@thisanimus
Created September 21, 2020 13:40
Show Gist options
  • Save thisanimus/c37d2b30536e44be7742f234028a77cb to your computer and use it in GitHub Desktop.
Save thisanimus/c37d2b30536e44be7742f234028a77cb to your computer and use it in GitHub Desktop.
Anchor Scroll
class AnchorScroll{
constructor(link){
this.link = link;
this.linkHref = this.link.getAttribute('href');
this.target = document.querySelector(linkHref);
this.init();
}
init(){
link.addEventListener('click', (e) => {
e.preventDefault();
this.target.scrollIntoView({
behavior: 'smooth',
block: 'start'
});
history.pushState(null, null, this.linkHref);
});
}
}
const anchorLinks = document.querySelectorAll('a[href^="#"]');
anchorLinks.forEach((link) => {
new AnchorScroll(link);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment