Skip to content

Commit 807ad79

Browse files
committed
fixed back to top button
1 parent 087bed0 commit 807ad79

3 files changed

Lines changed: 57 additions & 4 deletions

File tree

assets/css/style.css

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2051,4 +2051,36 @@ textarea.input-field {
20512051
left: 0;
20522052
}
20532053

2054-
}
2054+
}
2055+
2056+
2057+
2058+
2059+
.back-top-btn {
2060+
position: fixed;
2061+
bottom: 20px;
2062+
right: 20px;
2063+
background-color: var(--gold-crayola);
2064+
color: var(--smoky-black-1);
2065+
font-size: 22px;
2066+
width: 50px;
2067+
height: 50px;
2068+
border-radius: var(--radius-circle);
2069+
display: grid;
2070+
place-items: center;
2071+
box-shadow: var(--shadow-1);
2072+
transition: var(--transition-1);
2073+
opacity: 0;
2074+
visibility: hidden;
2075+
z-index: 4;
2076+
}
2077+
2078+
.back-top-btn:is(:hover, :focus-visible) {
2079+
background-color: var(--white);
2080+
color: var(--gold-crayola);
2081+
}
2082+
2083+
.back-top-btn.active {
2084+
opacity: 1;
2085+
visibility: visible;
2086+
}

assets/js/script.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,4 +143,24 @@ window.addEventListener("mousemove", function (event) {
143143
parallaxItems[i].style.transform = `translate3d(${x}px, ${y}px, 0px)`;
144144
}
145145

146-
});
146+
});
147+
148+
149+
150+
const backTopBtn = document.querySelector("[data-back-top-btn]");
151+
152+
const toggleButton = () => {
153+
if (window.scrollY > 100) {
154+
backTopBtn.classList.add("active");
155+
} else {
156+
backTopBtn.classList.remove("active");
157+
}
158+
};
159+
160+
window.addEventListener("scroll", toggleButton);
161+
162+
backTopBtn.addEventListener("click", () => {
163+
window.scrollTo({ top: 0, behavior: "smooth" });
164+
});
165+
166+

index.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,8 +1064,9 @@ <h3 class="card-title title-2 text-center">
10641064
</main>
10651065

10661066

1067-
1068-
1067+
<a href="#top" class="back-top-btn active" data-back-top-btn>
1068+
<ion-icon name="caret-up"></ion-icon>
1069+
</a>
10691070

10701071
<!--
10711072
- #FOOTER

0 commit comments

Comments
 (0)