Untitled

 avatar
unknown
javascript
a year ago
449 B
5
Indexable
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" });
});
Editor is loading...
Leave a Comment