✨feature: Update "back to top" button to work on scroll up after scroll down
This commit is contained in:
@@ -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' });
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user