wuxin0011`blog wuxin0011`blog
首页
  • 基础内容

    • HTML
    • CSS
    • JavaScript
  • 进阶

    • JavaScript教程
    • ES6 教程
    • Vue
    • React
    • Typescript
    • Git
  • 推荐阅读
  • java随笔
  • JVM (opens new window)
  • 分类
  • 标签
  • 归档
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
关于
  • git使用
  • docker部署项目
  • 错误收集
  • github-start
  • background-color
  • live-server
  • vscode-cpp-config
  • tampermonkey-script (opens new window)
  • leetcode-tool (opens new window)
  • 网站
  • 前端
  • 后端
  • Github Topic (opens new window)
GitHub (opens new window)

wuxin0011

懂得越多,懂得越少
首页
  • 基础内容

    • HTML
    • CSS
    • JavaScript
  • 进阶

    • JavaScript教程
    • ES6 教程
    • Vue
    • React
    • Typescript
    • Git
  • 推荐阅读
  • java随笔
  • JVM (opens new window)
  • 分类
  • 标签
  • 归档
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
关于
  • git使用
  • docker部署项目
  • 错误收集
  • github-start
  • background-color
  • live-server
  • vscode-cpp-config
  • tampermonkey-script (opens new window)
  • leetcode-tool (opens new window)
  • 网站
  • 前端
  • 后端
  • Github Topic (opens new window)
GitHub (opens new window)
  • HTML

  • CSS

  • JavaScript文章

  • 学习笔记

  • 前端随笔

    • css之bem架构
    • vue3自定义权限指令
    • 使用Gtihub api 获取用户star项目
    • 使用免费云函数系统搭建评论系统
    • 主题快照
      • 摘要
      • 内容
      • 演示
      • 相关连接
    • 静态前端项目密码破解
  • 前端
  • 前端随笔
wuxin0011
2023-05-25
目录

主题快照

# 摘要

五一的时候在B站看了下 antfu (opens new window) 大佬直播,内容是一个**屏幕快照功能 (opens new window)**,看了下,代码不多,蛮简单的,但是特效好啊😂。

不过只有最新浏览器才支持,比如火狐浏览器暂时是不支持的😅。

录播传送门 (opens new window)。

# 内容

ts

你你可以把它当成一个 Hook

/**
 * 主题动画切换
 * @see https://developer.mozilla.org/en-US/docs/Web/API/View_Transitions_API
 * @see https://github.com/vuejs/vitepress/pull/2347/files
 * @param event event
 */
const toggleColorMode = (event?: MouseEvent): void => {

  // @ts-expect-error experimental API
  const isAppearanceTransition = document.startViewTransition && !window.matchMedia('(prefers-reduced-motion: reduce)').matches

  // 如果不支持
  if (!isAppearanceTransition || !event) {
    // 在这里修改主题 themeUpdate()
    themeUpdate()
    return
  }
  const x = event.clientX
  const y = event.clientY
  const endRadius = Math.hypot(
    Math.max(x, innerWidth - x),
    Math.max(y, innerHeight - y),
  )
  // @ts-expect-error: Transition API
  const transition = document.startViewTransition(async () => {
    // 在这里修改主题 themeUpdate()
    await nextTick()
  })
  transition.ready
    .then(() => {
      const clipPath = [
        `circle(0px at ${x}px ${y}px)`,
        `circle(${endRadius}px at ${x}px ${y}px)`,
      ]
      document.documentElement.animate(
        {
          clipPath: isDarkMode.value
            ? [...clipPath].reverse()
            : clipPath,
        },
        {
          duration: 400,
          easing: 'ease-out',
          pseudoElement: isDarkMode.value
            ? '::view-transition-old(root)'
            : '::view-transition-new(root)',
        },
      )
    })
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

点击查看更多 (opens new window)

css

吧下面代码块放在 index.css这种

/* 主题动画切换 */
::view-transition-old(root),
::view-transition-new(root) {
  animation: none;
  mix-blend-mode: normal;
}
::view-transition-old(root) {
  z-index: 1;
}
::view-transition-new(root) {
  z-index: 100;
}
.dark::view-transition-old(root) {
  z-index: 100;
}
.dark::view-transition-new(root) {
  z-index: 1;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

注意: 如果出现样式问题,请调整 z-index

使用方式

<script setup>
    // 引入hook
    import { toggleColorMode } from 'xxxx'
 </script>

<template>
    <button @click="toggleColorMode">
        click me!
    </button>
</tempalte>
1
2
3
4
5
6
7
8
9
10

# 演示

点击查看主题演示 (opens new window)

动画

# 相关连接

  • 屏幕录制工具 (opens new window)
  • Gif工具 (opens new window)
  • vitepress pull (opens new window)
  • 快照api (opens new window)
  • antfu 录播 (opens new window)
编辑 (opens new window)
上次更新: 2025-06-05, 08:31:31
使用免费云函数系统搭建评论系统
静态前端项目密码破解

← 使用免费云函数系统搭建评论系统 静态前端项目密码破解→

最近更新
01
vscode配置c++环境
05-04
02
LeetCode刷题工具之本地对拍
04-04
03
sublime对应语言模板使用技巧
02-28
更多文章>
Theme by Vdoing | Copyright © 2020-2025 wuxin0011 | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式