dotfiles
My PC Dotfiles & Websitegit clone https://github.com/maydayv7/dotfiles
site/static/js/scroll.js (798B)
1var scrollButton = document.querySelector(".scroll-back");
2var rootElement = document.documentElement;
3
4if (scrollButton) {
5 function handleScroll() {
6 var scrollTotal = rootElement.scrollHeight - rootElement.clientHeight;
7 if (rootElement.scrollTop / scrollTotal > 0.5) {
8 scrollButton.style.visibility = "visible";
9 scrollButton.style.opacity = 100;
10 } else {
11 scrollButton.style.visibility = "hidden";
12 scrollButton.style.opacity = 0;
13 }
14 }
15
16 function scrollBack() {
17 rootElement.scrollTo({
18 top: 0,
19 behavior: matchMedia("(prefers-reduced-motion: reduce)").matches
20 ? "instant"
21 : "smooth",
22 });
23 }
24
25 scrollButton.addEventListener("click", scrollBack);
26 document.addEventListener("scroll", handleScroll, { passive: true });
27}