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;
    });
  });
};
相关推荐
刘发财15 小时前
弃用html2pdf.js,这个html转pdf方案能力是它的几十倍
前端·javascript·github
牛奶17 小时前
2026年大模型怎么选?前端人实用对比
前端·人工智能·ai编程
牛奶17 小时前
前端人为什么要学AI?
前端·人工智能·ai编程
Kagol20 小时前
🎉OpenTiny NEXT-SDK 重磅发布:四步把你的前端应用变成智能应用!
前端·开源·agent
GIS之路21 小时前
ArcGIS Pro 中的 notebook 初识
前端
JavaGuide21 小时前
7 道 RAG 基础概念知识点/面试题总结
前端·后端
ssshooter21 小时前
看完就懂 useSyncExternalStore
前端·javascript·react.js
格砸1 天前
从入门到辞职|从ChatGPT到OpenClaw,跟上智能时代的进化
前端·人工智能·后端
Live000001 天前
在鸿蒙中使用 Repeat 渲染嵌套列表,修改内层列表的一个元素,页面不会更新
前端·javascript·react native
柳杉1 天前
使用Ai从零开发智慧水利态势感知大屏(开源)
前端·javascript·数据可视化