Skip to content

Commit dcb0512

Browse files
committed
feat: 新增切换主题时的快照动画
1 parent 117d956 commit dcb0512

File tree

4 files changed

+42
-3
lines changed

4 files changed

+42
-3
lines changed

src/common/assets/styles/index.css

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/* 全局 CSS 变量 */
22
@import url("./variables.css");
3+
/* View Transition */
4+
@import url("./view-transition.css");
35

46
html {
57
height: 100%;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/* 控制切换主题时的动画效果(只在较新的浏览器上生效,例如 Chrome 111+) */
2+
3+
::view-transition-old(root) {
4+
animation: none;
5+
mix-blend-mode: normal;
6+
}
7+
8+
::view-transition-new(root) {
9+
animation: 0.5s ease-in clip-animation;
10+
mix-blend-mode: normal;
11+
}
12+
13+
@keyframes clip-animation {
14+
from {
15+
clip-path: circle(0px at var(--mobvue-dark-x) var(--mobvue-dark-y));
16+
}
17+
to {
18+
clip-path: circle(var(--mobvue-dark-r) at var(--mobvue-dark-x) var(--mobvue-dark-y));
19+
}
20+
}

src/common/composables/useDark.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,23 @@
11
import { getIsDark, setIsDark } from "@@/utils/cache/local-storage"
2+
import { setCssVar } from "@@/utils/css"
23

34
const isDark = ref<boolean>(getIsDark() === "true")
45

6+
function changeDark({ clientX, clientY }: MouseEvent) {
7+
const maxRadius = Math.hypot(
8+
Math.max(clientX, window.innerWidth - clientX),
9+
Math.max(clientY, window.innerHeight - clientY)
10+
)
11+
const documentElement = document.documentElement
12+
setCssVar("--mobvue-dark-x", `${clientX}px`, documentElement)
13+
setCssVar("--mobvue-dark-y", `${clientY}px`, documentElement)
14+
setCssVar("--mobvue-dark-r", `${maxRadius}px`, documentElement)
15+
const handler = () => {
16+
isDark.value = !isDark.value
17+
}
18+
document.startViewTransition ? document.startViewTransition(handler) : handler()
19+
}
20+
521
/** 初始化 */
622
function initDark() {
723
// watchEffect 来收集副作用
@@ -11,6 +27,7 @@ function initDark() {
1127
})
1228
}
1329

30+
/** 黑暗模式 Composable */
1431
export function useDark() {
15-
return { isDark, initDark }
32+
return { isDark, changeDark, initDark }
1633
}

src/pages/me/index.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const router = useRouter()
77
88
const userStore = useUserStore()
99
10-
const { isDark } = useDark()
10+
const { isDark, changeDark } = useDark()
1111
1212
function onClick() {
1313
showToast("更多功能,敬请期待 ~")
@@ -37,7 +37,7 @@ function onLogout() {
3737
<van-cell-group un-mb-8px>
3838
<van-cell title="🌗 Dark Mode 黑暗模式" center>
3939
<template #right-icon>
40-
<van-switch v-model="isDark" size="min(5.333vw, 40px)" />
40+
<van-switch :model-value="isDark" size="min(5.333vw, 40px)" @click="changeDark" />
4141
</template>
4242
</van-cell>
4343
<van-cell title="📚 Documentation 文档教程" is-link url="https://juejin.cn/column/7472609448201666599" />

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy