fix: skip scroll event if no backToTop element (#138)

This commit is contained in:
Xin 2023-10-12 23:25:34 +01:00 committed by GitHub
parent 96c6ff073f
commit cb09b7ce1e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,18 +1,22 @@
const backToTop = document.querySelector("#backToTop"); // Back to top button
document.addEventListener("scroll", (event) => { document.addEventListener("DOMContentLoaded", function () {
if (window.scrollY > 300) { const backToTop = document.querySelector("#backToTop");
backToTop.classList.remove("opacity-0"); if (backToTop) {
} else { document.addEventListener("scroll", (e) => {
backToTop.classList.add("opacity-0"); if (window.scrollY > 300) {
backToTop.classList.remove("opacity-0");
} else {
backToTop.classList.add("opacity-0");
}
});
} }
}); });
function scrollUp() { function scrollUp() {
window.scroll({ window.scroll({
top: 0, top: 0,
left: 0, left: 0,
behavior: 'smooth' behavior: "smooth",
}); });
} }