Skip to content

Commit 91fb70b

Browse files
committed
修复明暗主题切换动画闪烁
1 parent fdfbafe commit 91fb70b

File tree

1 file changed

+12
-5
lines changed
  • src/layouts/components/Topbar/Toolbar/ColorScheme

1 file changed

+12
-5
lines changed

src/layouts/components/Topbar/Toolbar/ColorScheme/index.vue

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,35 @@ function toggleColorScheme(event: MouseEvent) {
1010
settingsStore.currentColorScheme && settingsStore.setColorScheme(settingsStore.currentColorScheme === 'dark' ? 'light' : 'dark')
1111
return
1212
}
13-
const x = event.clientX
14-
const y = event.clientY
13+
const target = event.target as HTMLElement
14+
const { left, top, width, height } = target.getBoundingClientRect()
15+
const x = left + width / 2
16+
const y = top + height / 2
1517
const endRadius = Math.hypot(
1618
Math.max(x, innerWidth - x),
1719
Math.max(y, innerHeight - y),
1820
)
21+
const ratioX = (100 * x) / innerWidth
22+
const ratioY = (100 * y) / innerHeight
23+
const referR = Math.hypot(innerWidth, innerHeight) / Math.SQRT2
24+
const ratioR = (100 * endRadius) / referR
1925
const transition = document.startViewTransition(async () => {
2026
settingsStore.currentColorScheme && settingsStore.setColorScheme(settingsStore.currentColorScheme === 'dark' ? 'light' : 'dark')
2127
await nextTick()
2228
})
2329
transition.ready.then(() => {
2430
const clipPath = [
25-
`circle(0px at ${x}px ${y}px)`,
26-
`circle(${endRadius}px at ${x}px ${y}px)`,
31+
`circle(0% at ${ratioX}% ${ratioY}%)`,
32+
`circle(${ratioR}% at ${ratioX}% ${ratioY}%)`,
2733
]
2834
document.documentElement.animate(
2935
{
30-
clipPath: settingsStore.currentColorScheme === 'light' ? clipPath : clipPath.reverse(),
36+
clipPath: settingsStore.currentColorScheme === 'light' ? clipPath : [...clipPath].reverse(),
3137
},
3238
{
3339
duration: 500,
3440
easing: 'ease-in-out',
41+
fill: 'both',
3542
pseudoElement: settingsStore.currentColorScheme === 'light' ? '::view-transition-new(root)' : '::view-transition-old(root)',
3643
},
3744
)

0 commit comments

Comments
 (0)