|
| 1 | +--- |
| 2 | +import IconChevronLeft from "@/assets/icons/IconChevronLeft.svg"; |
| 3 | +
|
| 4 | +/* |
| 5 | +In #btt-btn-container, left position is `left-[calc((var(--container-3xl)/2)+50%+1rem)]`. |
| 6 | +This is because we use `max-w-3xl` in `PostDetails.astro`. |
| 7 | +If `max-width` in `PostDetails.astro` get updated, make sure to update the `var(--container-3xl)` as well. |
| 8 | +max-w-3xl => var(--container-3xl) |
| 9 | +max-w-4xl => var(--container-4xl) |
| 10 | +max-w-5xl => var(--container-5xl) |
| 11 | +etc... |
| 12 | +*/ |
| 13 | +--- |
| 14 | + |
| 15 | +<div |
| 16 | + id="btt-btn-container" |
| 17 | + class:list={[ |
| 18 | + "fixed right-4 bottom-8 xl:right-auto xl:left-[calc((var(--container-3xl)/2)+50%+1rem)]", |
| 19 | + "translate-y-14 opacity-0 transition-all duration-500", |
| 20 | + ]} |
| 21 | +> |
| 22 | + <button |
| 23 | + data-button="back-to-top" |
| 24 | + class:list={[ |
| 25 | + "group relative", |
| 26 | + "size-14 rounded-full px-2 py-1 shadow-xl lg:h-8 lg:w-fit lg:rounded-md lg:shadow-none", |
| 27 | + "bg-background lg:bg-transparent", |
| 28 | + ]} |
| 29 | + > |
| 30 | + <span |
| 31 | + id="progress-indicator" |
| 32 | + class="absolute inset-0 -z-10 block size-14 scale-110 rounded-full bg-transparent lg:hidden lg:h-8 lg:rounded-md" |
| 33 | + ></span> |
| 34 | + <IconChevronLeft class="inline-block rotate-90 lg:hidden" /> |
| 35 | + <span |
| 36 | + class="sr-only underline decoration-dashed decoration-2 underline-offset-4 group-hover:text-accent lg:not-sr-only" |
| 37 | + > |
| 38 | + <span class="text-xl" tabindex="0">↑</span> Back To Top |
| 39 | + </span> |
| 40 | + </button> |
| 41 | +</div> |
| 42 | + |
| 43 | +<script is:inline data-astro-rerun> |
| 44 | + /** Scrolls the document to the top when |
| 45 | + * the "Back to Top" button is clicked. */ |
| 46 | + function backToTop() { |
| 47 | + const rootElement = document.documentElement; |
| 48 | + const btnContainer = document.querySelector("#btt-btn-container"); |
| 49 | + const backToTopBtn = document.querySelector("[data-button='back-to-top']"); |
| 50 | + const progressIndicator = document.querySelector("#progress-indicator"); |
| 51 | + |
| 52 | + if (!rootElement || !btnContainer || !backToTopBtn || !progressIndicator) |
| 53 | + return; |
| 54 | + |
| 55 | + // Attach click event handler for back-to-top button |
| 56 | + backToTopBtn.addEventListener("click", () => { |
| 57 | + document.body.scrollTop = 0; // For Safari |
| 58 | + document.documentElement.scrollTop = 0; // For Chrome, Firefox, IE and Opera |
| 59 | + }); |
| 60 | + |
| 61 | + // Handle button visibility according to scroll position |
| 62 | + let lastVisible = null; |
| 63 | + |
| 64 | + function handleScroll() { |
| 65 | + const scrollTotal = rootElement.scrollHeight - rootElement.clientHeight; |
| 66 | + const scrollTop = rootElement.scrollTop; |
| 67 | + const scrollPercent = Math.floor((scrollTop / scrollTotal) * 100); |
| 68 | + |
| 69 | + progressIndicator.style.setProperty( |
| 70 | + "background-image", |
| 71 | + `conic-gradient(var(--accent), var(--accent) ${scrollPercent}%, transparent ${scrollPercent}%)` |
| 72 | + ); |
| 73 | + |
| 74 | + const isVisible = scrollTop / scrollTotal > 0.3; |
| 75 | + |
| 76 | + if (isVisible !== lastVisible) { |
| 77 | + btnContainer.classList.toggle("opacity-100", isVisible); |
| 78 | + btnContainer.classList.toggle("translate-y-0", isVisible); |
| 79 | + btnContainer.classList.toggle("opacity-0", !isVisible); |
| 80 | + btnContainer.classList.toggle("translate-y-14", !isVisible); |
| 81 | + lastVisible = isVisible; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + let ticking = false; |
| 86 | + document.addEventListener("scroll", () => { |
| 87 | + if (!ticking) { |
| 88 | + window.requestAnimationFrame(() => { |
| 89 | + handleScroll(); |
| 90 | + ticking = false; |
| 91 | + }); |
| 92 | + ticking = true; |
| 93 | + } |
| 94 | + }); |
| 95 | + } |
| 96 | + |
| 97 | + backToTop(); |
| 98 | +</script> |
0 commit comments