55 lines
1.7 KiB
JavaScript
55 lines
1.7 KiB
JavaScript
(function() {
|
|
'use strict';
|
|
|
|
var transitionMs = 120;
|
|
|
|
function prefersReducedMotion() {
|
|
return window.matchMedia && window.matchMedia('(prefers-reduced-motion: reduce)').matches;
|
|
}
|
|
|
|
function isInternalNavigation(link) {
|
|
if (!link || !link.href || link.target || link.hasAttribute('download')) return false;
|
|
|
|
var targetUrl;
|
|
try {
|
|
targetUrl = new URL(link.href, window.location.href);
|
|
} catch (_error) {
|
|
return false;
|
|
}
|
|
|
|
if (targetUrl.origin !== window.location.origin) return false;
|
|
if (targetUrl.pathname === window.location.pathname && targetUrl.search === window.location.search) {
|
|
return targetUrl.hash && targetUrl.hash !== window.location.hash;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
function initPageTransitions() {
|
|
if (prefersReducedMotion()) return;
|
|
|
|
document.addEventListener('click', function(event) {
|
|
if (event.defaultPrevented || event.button !== 0 || event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) {
|
|
return;
|
|
}
|
|
|
|
var link = event.target.closest ? event.target.closest('a[href]') : null;
|
|
if (!isInternalNavigation(link)) return;
|
|
|
|
var targetUrl = new URL(link.href, window.location.href);
|
|
if (targetUrl.pathname === window.location.pathname && targetUrl.search === window.location.search) return;
|
|
|
|
event.preventDefault();
|
|
document.body.classList.add('page-leaving');
|
|
window.setTimeout(function() {
|
|
window.location.href = targetUrl.href;
|
|
}, transitionMs);
|
|
});
|
|
}
|
|
|
|
if (document.readyState === 'loading') {
|
|
document.addEventListener('DOMContentLoaded', initPageTransitions, { once: true });
|
|
} else {
|
|
initPageTransitions();
|
|
}
|
|
})();
|