vue3+element-plus暗黑模式切换动画圆弧过渡
vue3+element-plus暗黑模式切换动画圆弧过渡
效果
html
<div class="toggle" ref="switchRef" @click.stop="toggleDark()">
<el-icon v-show="!isDark" :size="30"><Moon /></el-icon>
<el-icon v-show="isDark" :size="30"><Sunny /></el-icon>
</div>
ts
import { useDark } from '@vueuse/core';
const isDark = useDark();
//获取切换元素的ref
const switchRef = ref<HTMLElement>();
const toggleDark = () => {
// 若浏览器不支持 View Transitions
if (!document.startViewTransition) {
return true;
}
return new Promise(resolve => {
const switchEl = switchRef.value as HTMLElement;
const rect = switchEl.getBoundingClientRect();
const x = rect.left + rect.width / 2;
const y = rect.top + rect.height / 2;
const radius = Math.hypot(Math.max(x, innerWidth - x), Math.max(y, innerHeight - y));
const transition = document.startViewTransition(() => {
resolve(true);
});
transition.ready.then(() => {
const clipPath = [`circle(0px at ${x}px ${y}px)`, `circle(${radius}px at ${x}px ${y}px)`];
document.documentElement.animate(
{
clipPath,
},
{
duration: 400,
easing: 'ease-in',
pseudoElement: '::view-transition-new(root)',
}
);
isDark.value = !isDark.value;
});
});
};