Skip to content

Commit

Permalink
types update
Browse files Browse the repository at this point in the history
  • Loading branch information
rishab committed Nov 8, 2024
1 parent 9dbe72c commit c377bb2
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions src/components/MoveToTopButton/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,26 @@ const { class: customClass = "" } = Astro.props;
</a>

<script>
document.addEventListener("DOMContentLoaded", () => {
const moveToTopButton = document.getElementById("moveToTop");
const moveToTopButton = document.getElementById("moveToTop");

// Function to toggle visibility based on scroll position
const toggleVisibility = () => {
if (window.scrollY > 100) {
moveToTopButton.classList.remove("hidden");
// Ensure the element exists before accessing its methods
if (moveToTopButton) {
// Show the button when the user scrolls 100px down from the top
window.onscroll = function () {
if (document.body.scrollTop > 100 || document.documentElement.scrollTop > 100) {
moveToTopButton.classList.add("visible");
moveToTopButton.classList.remove("hidden");
} else {
moveToTopButton.classList.add("hidden");
moveToTopButton.classList.remove("visible");
}
};

// Attach scroll event listener
window.addEventListener("scroll", toggleVisibility);

// Smooth scroll to top
// Scroll smoothly to the top when the button is clicked
moveToTopButton.addEventListener("click", (event) => {
event.preventDefault();
event.preventDefault(); // Prevent default anchor behavior
window.scrollTo({ top: 0, behavior: "smooth" });
});
});
}
</script>

0 comments on commit c377bb2

Please sign in to comment.