mirror of
https://github.com/ChenQihan666/MyBlog-Next.git
synced 2026-08-14 11:29:33 +08:00
feat: improve back-to-top button behavior (#520)
Make the back-to-top button appear based on scroll position_ improving accessibility and UX.
This commit is contained in:
@@ -8,6 +8,7 @@ import Datetime from "@/components/Datetime.astro";
|
||||
import EditPost from "@/components/EditPost.astro";
|
||||
import ShareLinks from "@/components/ShareLinks.astro";
|
||||
import BackButton from "@/components/BackButton.astro";
|
||||
import BackToTopButton from "@/components/BackToTopButton.astro";
|
||||
import { getPath } from "@/utils/getPath";
|
||||
import { slugifyStr } from "@/utils/slugify";
|
||||
import IconChevronLeft from "@/assets/icons/IconChevronLeft.svg";
|
||||
@@ -109,22 +110,16 @@ const nextPost =
|
||||
|
||||
<EditPost class="sm:hidden" {hideEditPost} {post} />
|
||||
|
||||
<ul class="mt-4 mb-8 sm:my-8">
|
||||
{tags.map(tag => <Tag tag={slugifyStr(tag)} tagName={tag} />)}
|
||||
</ul>
|
||||
<BackToTopButton />
|
||||
|
||||
<div
|
||||
class="flex flex-col items-center justify-between gap-6 sm:flex-row sm:items-end sm:gap-4"
|
||||
class="flex flex-col items-center justify-between gap-6 sm:flex-row sm:items-start sm:gap-8"
|
||||
>
|
||||
<ShareLinks />
|
||||
<ul class="self-start">
|
||||
{tags.map(tag => <Tag tag={slugifyStr(tag)} tagName={tag} />)}
|
||||
</ul>
|
||||
|
||||
<button
|
||||
id="back-to-top"
|
||||
class="focus-outline py-1 whitespace-nowrap hover:opacity-75"
|
||||
>
|
||||
<IconChevronLeft class="inline-block rotate-90" />
|
||||
<span>Back to Top</span>
|
||||
</button>
|
||||
<ShareLinks />
|
||||
</div>
|
||||
|
||||
<hr class="my-6 border-dashed" />
|
||||
@@ -270,16 +265,6 @@ const nextPost =
|
||||
}
|
||||
attachCopyButtons();
|
||||
|
||||
/** Scrolls the document to the top when
|
||||
* the "Back to Top" button is clicked. */
|
||||
function backToTop() {
|
||||
document.querySelector("#back-to-top")?.addEventListener("click", () => {
|
||||
document.body.scrollTop = 0; // For Safari
|
||||
document.documentElement.scrollTop = 0; // For Chrome, Firefox, IE and Opera
|
||||
});
|
||||
}
|
||||
backToTop();
|
||||
|
||||
/* Go to page start after page swap */
|
||||
document.addEventListener("astro:after-swap", () =>
|
||||
window.scrollTo({ left: 0, top: 0, behavior: "instant" })
|
||||
|
||||
Reference in New Issue
Block a user