Files

83 lines
1.7 KiB
JavaScript

/* Lenis smooth scroll from trivelta-html: lerp 0.09, wheel only.
Same-page hashes use absolute URLs in WordPress, so those scroll too. */
(function () {
var reduceMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
if (reduceMotion || !window.Lenis) {
return;
}
var lenis = new window.Lenis({
lerp: 0.09,
smoothWheel: true,
smoothTouch: false
});
function raf(time) {
lenis.raf(time);
window.requestAnimationFrame(raf);
}
window.requestAnimationFrame(raf);
function anchorOffset() {
var header = document.querySelector('header');
if (!header) {
return -70;
}
var position = window.getComputedStyle(header).position;
if (position === 'fixed' || position === 'sticky') {
return -Math.round(header.getBoundingClientRect().height);
}
return 0;
}
document.addEventListener('click', function (event) {
if (event.defaultPrevented || event.button) {
return;
}
if (event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) {
return;
}
var anchor = event.target.closest ? event.target.closest('a') : null;
if (!anchor || (anchor.target && anchor.target !== '_self')) {
return;
}
var url;
try {
url = new URL(anchor.href, window.location.href);
} catch (error) {
return;
}
if (!url.hash || url.hash === '#') {
return;
}
if (url.origin !== window.location.origin || url.pathname !== window.location.pathname || url.search !== window.location.search) {
return;
}
var target = document.querySelector(url.hash);
if (!target) {
return;
}
event.preventDefault();
lenis.scrollTo(target, {
offset: anchorOffset(),
duration: 1.1
});
});
}());