Untitled

mail@pastecode.io avatar
unknown
javascript
a month ago
449 B
3
Indexable
Never
window.addEventListener("scroll", function() {
  const backToTopButton = document.getElementById("back-to-top");
  const scrollPosition = window.scrollY;

  if (scrollPosition > 100) { // Show button after 100px scroll
    backToTopButton.style.opacity = 1;
  } else {
    backToTopButton.style.opacity = 0;
  }
});

document.getElementById("back-to-top").addEventListener("click", function() {
  window.scrollTo({ top: 0, behavior: "smooth" });
});
Leave a Comment