I see more and more designs with side scroll menus, so I figured we need a component that automatically scrolls the menu to our current link.
Our code waits for the page to load, and then checks for the current link's offset from the left, and then scrolls the menu in that amount. I added 20px padding for the menu, thus the "-20px"
Add this code to every page the menu appears on, at the page settings section "Before </body> tag"
<script>
window.addEventListener('DOMContentLoaded', (event) => {
const scrollDiv = document.getElementById('scrollMenu');
const links = document.querySelectorAll('.link.w--current');
const currentLink = links[0];
const currentLinkOffset = links[0].offsetLeft - 20;
scrollDiv.scrollLeft = currentLinkOffset;
});
</script>
Whenever possible, use Class Names to target the elements in your interaction. You never know when you'll need to duplicate and use it somewhere else...