removed backToTop.js

This commit is contained in:
Steven Lynn
2024-08-06 21:37:17 +08:00
parent 348192f1b9
commit 2007b6392f
3 changed files with 53 additions and 36 deletions

View File

@@ -134,22 +134,59 @@
height: 50px;
border-radius: 50%;
border: none;
cursor: pointer;
display: none; /* 初始状态隐藏 */
display: flex;
align-items: center;
justify-content: center;
font-size: 24px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
text-decoration: none;
opacity: 0;
transition:
opacity 0.3s,
transform 0.3s;
cursor: pointer;
z-index: 1000;
transition: all 0.3s ease; /* 添加过渡效果 */
}
/* 使用 @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: #d0c3b4; /* 稍微深一点的颜色作为悬停效果 */
transform: translateY(-3px); /* 悬停时稍微上移 */
background-color: #b0a295;
transform: translateY(-3px);
}
#back-to-top:active {
transform: translateY(1px); /* 点击时下移,创造按压效果 */
box-shadow: 0 1px 5px rgba(0, 0, 0, 0.2); /* 减小阴影 */
transform: translateY(1px);
}
/* 为不支持 @scroll-timeline 的浏览器提供的回退样式 */
@supports not (animation-timeline: scroll()) {
#back-to-top {
display: none;
}
}
/* CSS Scroll Behavior 实现返回顶部的平滑滚动 */
html {
scroll-behavior: smooth;
}

View File

@@ -73,8 +73,7 @@ const FOOTER_INJECT = getEnv(import.meta.env, Astro, 'FOOTER_INJECT')
{...seoParams}
/>
<Fragment set:html={HEADER_INJECT} />
<script src="src/lib/backToTop.js" type="module"></script></head
>
</head>
<body>
<div id="wrapper">
@@ -125,9 +124,13 @@ const FOOTER_INJECT = getEnv(import.meta.env, Astro, 'FOOTER_INJECT')
</div>
</div>
</div>
<button id="back-to-top" aria-label="Back to top" title="Back to top">
<a
href="#wrapper"
id="back-to-top"
aria-label="Back to top"
title="Back to top"
>
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
@@ -137,7 +140,7 @@ const FOOTER_INJECT = getEnv(import.meta.env, Astro, 'FOOTER_INJECT')
>
<polyline points="18 15 12 9 6 15"></polyline>
</svg>
</button>
</a>
<Fragment set:html={FOOTER_INJECT} />
</body>
</html>

View File

@@ -1,23 +0,0 @@
function setupBackToTop() {
const backToTopButton = document.querySelector('#back-to-top')
// Toggle button visibility based on scroll position
const toggleBackToTopButton = () => {
backToTopButton.style.display = window.scrollY > 300 ? 'flex' : 'none'
}
// Attach scroll event listener to window
window.addEventListener('scroll', toggleBackToTopButton)
toggleBackToTopButton() // Initial check for button visibility
// Smooth scroll to top when button is clicked
backToTopButton.addEventListener('click', () => {
window.scrollTo({
top: 0,
behavior: 'smooth',
})
})
}
// Initialize the functionality once page is fully loaded
document.addEventListener('DOMContentLoaded', setupBackToTop)