-
Notifications
You must be signed in to change notification settings - Fork 166
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
39 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
}); | ||
} | ||
}); | ||
|
aae8a46
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this optimized version:
The back-to-top button element is cached outside the event listeners for better performance.
Throttling is implemented to limit the frequency of the scroll event listener function execution.
window.scrollY is used to directly get the vertical scroll position.
The visibility of the back-to-top button is controlled by toggling a "visible" class on the button element itself, improving CSS class management.
Accessibility is enhanced by focusing on an element after scrolling to the top, aiding users navigating with assistive technologies.
The code is encapsulated within a DOMContentLoaded event listener to ensure it runs after the DOM is fully loaded.
These improvements enhance the code's performance, readability, maintainability, and accessibility without hindering its main functionality.