From aae8a463b51e6d70570416a069193f073d18ef4c Mon Sep 17 00:00:00 2001 From: Ankush Srivastava Date: Fri, 10 May 2024 21:59:05 +0530 Subject: [PATCH] modified dashboard.js --- dashbboard.js | 54 +++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 39 insertions(+), 15 deletions(-) diff --git a/dashbboard.js b/dashbboard.js index 4482187..687fa15 100644 --- a/dashbboard.js +++ b/dashbboard.js @@ -1,20 +1,44 @@ -// Back-Top Button -document.addEventListener("scroll", function() { - const scrollTop = document.documentElement.scrollTop || document.body.scrollTop; +document.addEventListener("DOMContentLoaded", function() { + // Cache the back-to-top button element const backToTopButton = document.querySelector(".back-to-top"); - // show/hide back to top button based on scroll position - if (scrollTop > 50) { - document.body.classList.add("scroll-down"); - } else { - document.body.classList.remove("scroll-down"); + // Function to scroll to the top with smooth behavior + function scrollToTop() { + window.scrollTo({ + top: 0, + behavior: "smooth" + }); } -}); -// Function to scroll to the top when the button is clicked -function scrollToTop() { - window.scrollTo({ - top: 0, - behavior: "smooth" + // Function to handle scroll event with throttling + function handleScroll() { + const scrollTop = window.scrollY || document.documentElement.scrollTop; + if (scrollTop > 50) { + backToTopButton.classList.add("visible"); + } else { + backToTopButton.classList.remove("visible"); + } + } + + // Throttle the scroll event listener + let throttleTimeout; + function throttleScroll() { + if (!throttleTimeout) { + throttleTimeout = setTimeout(function() { + throttleTimeout = null; + handleScroll(); + }, 200); // Adjust the throttle interval as needed + } + } + + // Event listener for scrolling + window.addEventListener("scroll", throttleScroll); + + // Event listener for clicking back-to-top button + backToTopButton.addEventListener("click", function() { + scrollToTop(); + // Add focus to an element after scrolling to improve accessibility + document.querySelector("h1").focus(); // Example: focus on the page heading }); -} \ No newline at end of file +}); +