Skip to content

Instantly share code, notes, and snippets.

@iamrobert
Last active December 17, 2022 10:23
Show Gist options
  • Save iamrobert/a47a8a6f0f1814f8b2616ec912b969f8 to your computer and use it in GitHub Desktop.
Save iamrobert/a47a8a6f0f1814f8b2616ec912b969f8 to your computer and use it in GitHub Desktop.
Locomotive JS gists - #anchor scrolls and tabs
/* + LOCOMOTIVE ANCHORS
https://github.com/locomotivemtl/locomotive-scroll/issues/175#issuecomment-1082541029
======================================================================*/
const anchorLinks = document.querySelectorAll('.mainbody a[href^=\\#]:not(.no-loco):not([href$=\\#])');
anchorLinks.forEach((anchorLink) => {
let hashval = anchorLink.getAttribute('href');
let target = document.querySelector(hashval);
anchorLink.addEventListener('click', (e) => {
e.preventDefault();
e.stopPropagation();
locoScroll.scrollTo(target);
// Adding a JUMP STATUS
let targetID = '#' + target.id;
if (targetID !== "#") {
history.pushState(null, null, targetID);
}
//
});
});
/* + LOCOMOTIVE TABS
====================================================================== */
const tabsLinks = document.querySelectorAll('.mainbody a.js-tabs__trigger');
tabsLinks.forEach((tabsLink) => {
tabsLink.addEventListener('click', (e) => {
locoScroll.update();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment