Merge pull request #20 from stvlynn/main

add backToTop button
This commit is contained in:
面条
2024-08-08 21:00:47 +08:00
committed by GitHub
3 changed files with 83 additions and 1 deletions

View File

@@ -0,0 +1,10 @@
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round">
<polyline points="18 15 12 9 6 15"></polyline>
</svg>

After

Width:  |  Height:  |  Size: 244 B

View File

@@ -81,3 +81,70 @@
display: block;
}
}
#back-to-top {
position: fixed;
bottom: 20px;
right: 20px;
background-color: #dfd3c3;
color: #333;
width: 50px;
height: 50px;
border-radius: 50%;
border: none;
display: flex;
align-items: center;
justify-content: center;
font-size: 24px;
text-decoration: none;
opacity: 0;
transition:
opacity 0.3s,
transform 0.3s;
cursor: pointer;
z-index: 1000;
}
/* 使用 @scroll-timeline 控制按钮的显示 */
@supports (animation-timeline: scroll()) {
@scroll-timeline buttonVisibility {
source: auto;
orientation: vertical;
scroll-offsets: 0%, 100px;
}
#back-to-top {
animation: fadeIn 0.5s linear both;
animation-timeline: buttonVisibility;
}
@keyframes fadeIn {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
}
#back-to-top:hover {
background-color: #b0a295;
transform: translateY(-3px);
}
#back-to-top:active {
transform: translateY(1px);
}
/* 为不支持 @scroll-timeline 的浏览器提供的回退样式 */
@supports not (animation-timeline: scroll()) {
#back-to-top {
display: none;
}
}
/* CSS Scroll Behavior 实现返回顶部的平滑滚动 */
html {
scroll-behavior: smooth;
}

View File

@@ -42,6 +42,8 @@ const seoParams = {
const HEADER_INJECT = getEnv(import.meta.env, Astro, 'HEADER_INJECT')
const FOOTER_INJECT = getEnv(import.meta.env, Astro, 'FOOTER_INJECT')
const backToTopIcon = 'src/assets/back-to-top.svg'
---
<!doctype html>
@@ -74,6 +76,7 @@ const FOOTER_INJECT = getEnv(import.meta.env, Astro, 'FOOTER_INJECT')
/>
<Fragment set:html={HEADER_INJECT} />
</head>
<body>
<div id="wrapper">
<div id="container">
@@ -123,7 +126,9 @@ const FOOTER_INJECT = getEnv(import.meta.env, Astro, 'FOOTER_INJECT')
</div>
</div>
</div>
<a href="#wrapper" id="back-to-top" aria-label="Back to top">
<img src={backToTopIcon} alt="Back to Top" />
</a>
<Fragment set:html={FOOTER_INJECT} />
</body>
</html>