feature: Update "back to top" button to work on scroll up after scroll down

This commit is contained in:
Keith Solomon
2026-03-09 09:05:53 -05:00
parent 5b7fed2cb8
commit 9bde54a41c
+11 -1
View File
@@ -6,10 +6,20 @@ class BackToTopButton extends HTMLElement {
↑ Top
</button>
`;
const btn = this.querySelector('#backToTopBtn');
let previousScrollY = window.scrollY;
window.addEventListener('scroll', () => {
btn.style.display = window.scrollY > 300 ? 'block' : 'none';
const currentScrollY = window.scrollY;
const isScrollingUp = currentScrollY < previousScrollY;
const shouldShowButton = currentScrollY > 300 && isScrollingUp;
btn.style.display = shouldShowButton ? 'block' : 'none';
previousScrollY = currentScrollY;
});
btn.addEventListener('click', () => {
window.scrollTo({ top: 0, behavior: 'smooth' });
});