Files
webawesome/docs/assets/scripts/scroll.js

34 lines
994 B
JavaScript
Raw Normal View History

2024-04-17 11:20:27 -04:00
// Smooth links
document.addEventListener('click', event => {
const link = event.target.closest('a');
if (!link || link.getAttribute('data-smooth-link') === 'off') {
return;
}
2024-12-17 12:09:45 -05:00
const id = (link.hash ?? '').substr(1);
// Only handle smooth scroll if there's a hash and the link points to the current page
if (id && link.pathname === window.location.pathname) {
2024-04-17 11:20:27 -04:00
const target = document.getElementById(id);
const headerHeight = document.querySelector('wa-page > header').clientHeight;
if (target) {
event.preventDefault();
window.scroll({
top: target.offsetTop - headerHeight,
behavior: 'smooth',
2024-04-17 11:20:27 -04:00
});
history.pushState(undefined, undefined, `#${id}`);
}
}
});
// Scroll classes
function updateScrollClass() {
document.body.classList.toggle('scrolled-down', window.scrollY >= 10);
}
window.addEventListener('scroll', updateScrollClass);
window.addEventListener('turbo:render', updateScrollClass);
updateScrollClass();