vue3+element-plus暗黑模式切换动画圆弧过渡

vue3+element-plus暗黑模式切换动画圆弧过渡

效果

html

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

javascript 复制代码
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;
    });
  });
};
相关推荐
布列瑟农的星空4 分钟前
tree shaking为什么失效
前端
米丘7 分钟前
vue3.5.x 单文件组件(SFC)样式编译过程
css·vue.js·postcss
idcu13 分钟前
加入 Lyt.js:从第一行代码到核心贡献者
前端
最炫的美少女战士15 分钟前
claude code 安装报错claude native binary not installed.
javascript
肥羊zzz16 分钟前
Vue2 vs Vue3 中 v-for 的 key 用法对比
前端·vue.js
dsyyyyy11011 小时前
HTML总结
前端·html
用户2367829801681 小时前
Canvas:实现一个高颜值二维码生成器
javascript
剑神一笑1 小时前
从字符串到时间线:实现一个 Cron 表达式解析器
javascript·typescript·reactjs
前端那点事1 小时前
深度解析:Vue中computed的实现原理(易懂不晦涩)
前端·vue.js
Mike_jia2 小时前
PortNote:可视化端口管理工具,让端口冲突成为历史
前端