Go to top button (#975)

* go to top button

* refactoring
This commit is contained in:
trean
2024-06-17 18:03:07 +02:00
committed by GitHub
parent 731ce10360
commit 58f04a6781
7 changed files with 51 additions and 0 deletions
@@ -24,3 +24,24 @@ export function scrollIntoViewWithOffset(id, offset) {
export function isNodeList(list) {
return Object.prototype.isPrototypeOf.call(NodeList.prototype, list);
}
export function initGoToTopButton(selector) {
const button = document.querySelector(selector || '.go-to-top');
if (!button) {
return;
}
window.addEventListener('scroll', () => {
const shouldShow = window.scrollY > window.innerHeight / 3;
button.classList.toggle('d-block', shouldShow);
button.classList.toggle('d-none', !shouldShow);
});
button.addEventListener('click', () => {
window.scrollTo({
top: 0,
behavior: 'smooth'
});
});
}
@@ -1,5 +1,6 @@
import scrollHandler from './scroll-handler';
import { XXL_BREAKPOINT } from './constants';
import { initGoToTopButton } from './helpers';
// on document ready
document.addEventListener('DOMContentLoaded', function () {
@@ -75,4 +76,5 @@ document.addEventListener('DOMContentLoaded', function () {
}
});
initGoToTopButton('#scrollToTopBtn');
});