Skip to content

Commit 173975c

Browse files
committed
添加视图过渡动画
1 parent 3142b63 commit 173975c

File tree

3 files changed

+18
-15
lines changed

3 files changed

+18
-15
lines changed

src/components/HeadMenu.astro

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<a id="about" href="/about/" data-astro-prefetch>关于</a>
1212
</nav>
1313

14-
<script>
14+
<script is:inline>
1515
const updateSelected = () => {
1616
let rawPathname = window.location.pathname;
1717
const regex = /^\/([^\/]+)(?:\/|$)/;
@@ -23,6 +23,7 @@
2323
}
2424
e.classList.add("selected");
2525
};
26+
document.addEventListener("astro:after-swap", updateSelected); //视图过渡后的脚本执行
2627
updateSelected();
2728
</script>
2829

src/layouts/BaseLayout.astro

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
---
22
import HeadMenu from "../components/HeadMenu.astro";
33
import "../styles/global.css";
4+
import { ClientRouter } from "astro:transitions";
45
56
const { description, keywords ,selected} = Astro.props;
67
---
@@ -10,6 +11,7 @@ const { description, keywords ,selected} = Astro.props;
1011
<meta charset="utf-8" />
1112
<link rel="icon" href="/favicon.svg" type="image/svg" />
1213
<script src="../scripts/swUnregister.ts"/>
14+
<ClientRouter />
1315
<meta name="viewport" content="width=device-width, initial-scale=1" />
1416
<meta name="description" content={description} />
1517
<meta name="keywords" content={keywords} />
@@ -40,21 +42,21 @@ const { description, keywords ,selected} = Astro.props;
4042
</footer>
4143
</div>
4244
</body>
43-
44-
<script>
45-
let lastScrollTop = 0;
46-
window.addEventListener("scroll", () => {
47-
const menu = document.getElementById("menu");
48-
if (!menu || !(menu instanceof HTMLElement)) {
49-
return;
50-
}
51-
const scrollTop = window.scrollY;
52-
menu.style.top = scrollTop > lastScrollTop ? "-85px" : "5px";
53-
lastScrollTop = scrollTop;
54-
});
55-
</script>
5645
</html>
5746

47+
<script>
48+
let lastScrollTop = 0;
49+
window.addEventListener("scroll", () => {
50+
const menu = document.getElementById("menu");
51+
if (!menu || !(menu instanceof HTMLElement)) {
52+
return;
53+
}
54+
const scrollTop = window.scrollY;
55+
menu.style.top = scrollTop > lastScrollTop ? "-85px" : "5px";
56+
lastScrollTop = scrollTop;
57+
});
58+
</script>
59+
5860
<style>
5961
* {
6062
box-sizing: border-box; /* 确保所有元素的宽高包含 padding 和 border */

src/posts/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ title: index
88

99
于是 24 年末改用 jekyll,用 markdown 随时随地有 git 就能写博客了,很方便。
1010

11-
后来想做自定义,但 Rudy 苦手,于是凭着自己超烂的前端水平构建起了如今的小站,**目前还在努力建设中**,总之欢迎你!
11+
后来想做自定义,但 Rudy 苦手,于是凭着自己超烂的前端水平构建起了如今的小站,**目前还在努力建设中**现在在做:**移动端适配**总之欢迎你!
1212

1313
累了吗,可以在下面的椅子上休息,坐多久都可以!
1414

0 commit comments

Comments
 (0)