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;
    });
  });
};
相关推荐
奇迹_h14 小时前
打造你的HTML5打地鼠游戏:零基础入门实践
前端
SuperEugene14 小时前
Vue生态精选篇:Element Plus 的“企业后台常用组件”用法扫盲
前端·vue.js·面试
Neptune114 小时前
JavaScript回归基本功之---类型判断--typeof篇
前端·javascript·面试
贾铭14 小时前
如何实现一个网页版的剪映(三)使用fabric.js绘制时间轴
前端·后端
进击的尘埃14 小时前
微前端沙箱隔离:qiankun 和 wujie 到底在争什么
javascript
子兮曰15 小时前
后端字段又改了?我撸了一个 BFF 数据适配器,从此再也不怕接口“屎山”!
前端·javascript·架构
万少17 小时前
使用Trae轻松安装openclaw的教程-附带免费token
前端·openai·ai编程
颜酱17 小时前
一步步实现字符串计算器:从「转整数」到「带括号与优化」
javascript·后端·算法
浪浪山_大橙子17 小时前
OpenClaw 十分钟快速,安装与接入完全指南 - 推荐使用trae 官方 skills 安装
前端·人工智能
忆江南17 小时前
iOS 可视化埋点与无痕埋点详解
前端