feat: add back to top button and group with theme switcher
This commit is contained in:
parent
c6db8689b4
commit
350f6f3865
2 changed files with 32 additions and 1 deletions
27
src/components/BackToTop.astro
Normal file
27
src/components/BackToTop.astro
Normal file
|
@ -0,0 +1,27 @@
|
|||
---
|
||||
|
||||
---
|
||||
<button id="toTopBtn" title="Go to top">Top</button>
|
||||
<script>
|
||||
// Get the button
|
||||
let toTopButton = document.getElementById("toTopBtn");
|
||||
|
||||
// When the user scrolls down from the top of the document, show the button
|
||||
window.addEventListener("scroll", ()=> {
|
||||
if (window.scrollY < document.documentElement.clientHeight) {
|
||||
toTopButton.classList.remove('fade-in');
|
||||
toTopButton.classList.add('fade-out');
|
||||
toTopButton.style.display = 'block';
|
||||
} else {
|
||||
toTopButton.classList.remove('fade-out');
|
||||
toTopButton.classList.remove('fade-in');
|
||||
}
|
||||
})
|
||||
|
||||
// When the user clicks on the button, scroll to the top of the document
|
||||
toTopButton.addEventListener("click", toTop);
|
||||
function toTop() {
|
||||
document.body.scrollTop = 0;
|
||||
document.documentElement.scrollTop = 0;
|
||||
}
|
||||
</script>
|
Loading…
Add table
Add a link
Reference in a new issue