104 lines
2.3 KiB
JavaScript
104 lines
2.3 KiB
JavaScript
/* Cinematic page transition (from trivelta-html).
|
|
WordPress prints internal links as absolute URLs, so same-origin navigations
|
|
are included. External, in-page, admin, and modified clicks are left alone. */
|
|
(function () {
|
|
var reduceMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
|
|
var leaveMs = 360;
|
|
|
|
function playEnter() {
|
|
if (reduceMotion) {
|
|
document.body.classList.add('is-entered');
|
|
return;
|
|
}
|
|
|
|
requestAnimationFrame(function () {
|
|
requestAnimationFrame(function () {
|
|
document.body.classList.add('is-entered');
|
|
});
|
|
});
|
|
}
|
|
|
|
if (/(?:^|[?&])(ct_builder|oxygen_iframe|ct_inner)=/.test(window.location.search)) {
|
|
document.body.classList.add('is-entered');
|
|
return;
|
|
}
|
|
|
|
playEnter();
|
|
|
|
function shouldSkip(anchor, event) {
|
|
if (!anchor) {
|
|
return true;
|
|
}
|
|
|
|
if (event.defaultPrevented || event.button) {
|
|
return true;
|
|
}
|
|
|
|
if (event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) {
|
|
return true;
|
|
}
|
|
|
|
if ((anchor.target && anchor.target !== '_self') || anchor.hasAttribute('download')) {
|
|
return true;
|
|
}
|
|
|
|
var href = anchor.getAttribute('href');
|
|
|
|
if (!href || href.charAt(0) === '#' || /^(mailto:|tel:|javascript:)/i.test(href)) {
|
|
return true;
|
|
}
|
|
|
|
var url;
|
|
|
|
try {
|
|
url = new URL(anchor.href, window.location.href);
|
|
} catch (error) {
|
|
return true;
|
|
}
|
|
|
|
if (url.origin !== window.location.origin) {
|
|
return true;
|
|
}
|
|
|
|
if (url.pathname.indexOf('/wp-admin') !== -1 || /\/wp-(login|comments-post)\.php$/.test(url.pathname)) {
|
|
return true;
|
|
}
|
|
|
|
if (/\.(pdf|zip|rar|7z|docx?|xlsx?|pptx?|jpe?g|png|gif|webp|svg|mp[34]|wav)(\?|$)/i.test(url.pathname)) {
|
|
return true;
|
|
}
|
|
|
|
if (url.pathname === window.location.pathname && url.search === window.location.search) {
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
function go(url) {
|
|
if (document.body.classList.contains('is-leaving')) {
|
|
return;
|
|
}
|
|
|
|
document.body.classList.add('is-leaving');
|
|
window.setTimeout(function () {
|
|
window.location.href = url;
|
|
}, reduceMotion ? 0 : leaveMs);
|
|
}
|
|
|
|
document.addEventListener('click', function (event) {
|
|
var anchor = event.target.closest ? event.target.closest('a') : null;
|
|
|
|
if (shouldSkip(anchor, event)) {
|
|
return;
|
|
}
|
|
|
|
event.preventDefault();
|
|
go(anchor.href);
|
|
});
|
|
|
|
window.addEventListener('pageshow', function () {
|
|
document.body.classList.remove('is-leaving');
|
|
});
|
|
}());
|