SanYeCao-blog/src/components/Spinner.astro

97 lines
2.5 KiB
Text
Raw Normal View History

2026-04-14 10:48:18 +00:00
<div id="page-loading-indicator" aria-hidden="true" transition:persist>
2026-04-13 20:47:23 +00:00
<span class="page-loading-spinner"></span>
</div>
<script is:inline>
(() => {
2026-04-14 12:32:18 +00:00
const KEY = "__page_loading_indicator_bound__";
if (window[KEY]) return;
window[KEY] = true;
2026-04-13 20:47:23 +00:00
let timer;
2026-04-14 12:32:18 +00:00
const getEl = () => document.getElementById("page-loading-indicator");
2026-04-13 20:47:23 +00:00
const show = () => {
clearTimeout(timer);
timer = setTimeout(() => {
2026-04-14 12:32:18 +00:00
const el = getEl();
2026-04-13 20:47:23 +00:00
if (el) el.classList.add("is-active");
}, 120);
};
const hide = () => {
clearTimeout(timer);
2026-04-14 12:32:18 +00:00
const el = getEl();
if (!el) return;
requestAnimationFrame(() => {
requestAnimationFrame(() => {
el.classList.remove("is-active");
});
});
2026-04-13 20:47:23 +00:00
};
document.addEventListener("astro:before-preparation", show);
document.addEventListener("astro:page-load", hide);
})();
</script>
<style is:global>
#page-loading-indicator {
position: fixed;
top: 1rem;
right: 1rem;
width: 2rem;
height: 2rem;
display: grid;
place-items: center;
2026-04-14 12:32:18 +00:00
transform: scale(1.3) translateY(-70px);
2026-04-13 20:47:23 +00:00
pointer-events: none;
2026-04-14 12:32:18 +00:00
transition: transform 160ms linear;
2026-04-13 20:47:23 +00:00
z-index: 9999;
2026-04-14 12:32:18 +00:00
will-change: transform;
2026-04-13 20:47:23 +00:00
}
#page-loading-indicator.is-active {
2026-04-14 12:32:18 +00:00
transform: scale(1) translateY(0);
2026-04-13 20:47:23 +00:00
}
.page-loading-spinner {
width: 1.4rem;
height: 1.4rem;
box-sizing: border-box;
border: 1.5px solid #7da2ff;
border-top-color: white;
border-radius: 50%;
background: transparent;
box-shadow:
0 0 0 1px #606060,
inset 0 0 0 1px #606060;
animation: page-loading-spin-rhythm 2.1s infinite;
transform-origin: center;
}
@keyframes page-loading-spin-rhythm {
0% {
transform: rotate(0deg);
animation-timing-function: cubic-bezier(0.25, 0.75, 0.35, 1);
}
52% {
2026-04-14 12:32:18 +00:00
transform: rotate(720deg);
2026-04-13 20:47:23 +00:00
animation-timing-function: cubic-bezier(0.55, 0.08, 0.78, 0.22);
}
100% {
2026-04-14 12:32:18 +00:00
transform: rotate(1080deg);
2026-04-13 20:47:23 +00:00
}
}
.dark .page-loading-spinner,
html.dark .page-loading-spinner {
border-color: #d8c7a1;
border-top-color: transparent;
}
</style>