Skip to content

Commit

Permalink
fix: skip scroll event if no backToTop element (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
imfing authored Oct 12, 2023
1 parent 96c6ff0 commit cb09b7c
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions assets/js/back-to-top.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
const backToTop = document.querySelector("#backToTop");
// Back to top button

document.addEventListener("scroll", (event) => {
if (window.scrollY > 300) {
backToTop.classList.remove("opacity-0");
} else {
backToTop.classList.add("opacity-0");
document.addEventListener("DOMContentLoaded", function () {
const backToTop = document.querySelector("#backToTop");
if (backToTop) {
document.addEventListener("scroll", (e) => {
if (window.scrollY > 300) {
backToTop.classList.remove("opacity-0");
} else {
backToTop.classList.add("opacity-0");
}
});
}
});


function scrollUp() {
window.scroll({
top: 0,
left: 0,
behavior: 'smooth'
top: 0,
left: 0,
behavior: "smooth",
});
}

0 comments on commit cb09b7c

Please sign in to comment.