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;
    });
  });
};
相关推荐
邂逅星河浪漫20 分钟前
【JavaScript】==和===区别详解
java·javascript·==·===
TON_G-T27 分钟前
useEffect为什么会触发死循环
java·服务器·前端
Aurorar0rua33 分钟前
CS50 x 2024 Notes C - 02
前端
海参崴-1 小时前
C++代码格式规范
java·前端·c++
Dxy12393102161 小时前
JavaScript 如何捕获异常:从基础到进阶的完整指南
开发语言·javascript·udp
谢尔登1 小时前
【React】setState 触发渲染的流程
前端·react.js·前端框架
摸鱼仙人~2 小时前
Vue中markdown-it基础使用教程
前端·javascript·vue.js
落魄江湖行2 小时前
入门篇二:Nuxt 4路由自动生成:告别手动配置路由的日子
前端·vue.js·typescript·nuxt4
CQU_JIAKE3 小时前
4.4【Q】
java·前端·javascript
小陈工3 小时前
Python Web开发入门(十二):使用Flask-RESTful构建API——让后端开发更优雅
开发语言·前端·python·安全·oracle·flask·restful