纸间把夜晚
留给阅读。
留给阅读。
圆形扩散换肤
Circle reveal theme toggle
点深色模式按钮,新的颜色从按钮那里画一个圆扩散开,铺满换肤区域
常见大白话搜法:切换深色模式时新颜色从按钮处圆形扩散开、暗黑模式切换时画一个圆铺满换肤区域、主题切换的圆形扩散过渡、点夜间模式按钮整页像水波一样换色
- 适用位置
- 导航、按钮
- 气质感觉
- 戏剧化、俏皮
- 触发方式
- 点击
- 适合网站
- 个人网站、博客、作品集
实现原理与无障碍底线
点按钮时用浏览器原生的 View Transitions:先给旧页面截一张图,换好新主题,再让新页面用圆形裁剪(clip-path: circle())从按钮的中心长大,半径长到按钮离最远的屏幕角那么远,正好盖满;换回去时反过来,旧页面的圆收回按钮;只改传入区域的 data-theme,不改变其他区域;不支持 View Transitions 的浏览器、开启"减少动态"时直接换主题。
适用场景:个人网站、博客、作品集顶上的深色、浅色模式切换按钮:换主题本来是一下子全变,从按钮扩散开看得出"是这个按钮换的",也更有记忆点
减少动态(prefers-reduced-motion):直接换主题,不扩散
移动端与触屏:和桌面一样;按钮在屏幕一角时圆要长得更大,时长可以长一点
关键可调参数
| 参数 | 键名 | 默认值 | 建议范围 | 说明 |
|---|---|---|---|---|
| 扩散的时长 | duration | 600ms | — | — |
| 缓动 | easing | cubic-bezier(0.4, 0, 0.2, 1) | — | — |
可直接复制给 AI(Cursor / Claude / v0)的提示词
请在我的项目里给导航加上「圆形扩散换肤」(Circle reveal theme toggle)效果:点按钮时用浏览器原生的 View Transitions:先给旧页面截一张图,换好新主题,再让新页面用圆形裁剪(clip-path: circle())从按钮的中心长大,半径长到按钮离最远的屏幕角那么远,正好盖满;换回去时反过来,旧页面的圆收回按钮;只改传入区域的 data-theme,不改变其他区域;不支持 View Transitions 的浏览器、开启"减少动态"时直接换主题。
参数:扩散的时长 600ms,缓动 cubic-bezier(0.4, 0, 0.2, 1)。
要求:保留原有的内容和布局;系统开启"减少动态"时直接显示最终状态;脚本出错或没有执行时内容仍然可见。
参考:https://donglang.linter.top/effects/theme-circle-reveal.md(原理、参数和参考代码)原生无依赖参考代码
CSS
/* 颜色只作用于 .tcr 及其子元素,不更改站点主题。可自行覆盖四个变量。 */
.tcr { --tcr-paper: #faf9f6; --tcr-ink: #24252b; background: var(--tcr-paper); color: var(--tcr-ink); }
.tcr[data-theme="dark"] { --tcr-paper: #24252b; --tcr-ink: #faf9f6; }
/* 只在本次原生转场期间关掉默认交叉淡化,旧/新截图的堆叠方向决定扩散还是收回。 */
html[data-tcr-direction]::view-transition-old(root),
html[data-tcr-direction]::view-transition-new(root) { animation: none; mix-blend-mode: normal; }
html[data-tcr-direction="dark"]::view-transition-old(root) { z-index: 1; }
html[data-tcr-direction="dark"]::view-transition-new(root) { z-index: 2; }
html[data-tcr-direction="light"]::view-transition-old(root) { z-index: 2; }
html[data-tcr-direction="light"]::view-transition-new(root) { z-index: 1; }JavaScript
// 圆形扩散换肤 · 原生 View Transitions,本站原创实现,没有复制第三方代码。
// root 是要换肤的区域(可传 document.documentElement),按钮必须是原生 button;只改 root 的 data-theme。
// 搭配 theme-circle-reveal.css,明/暗主题分别放在 .tcr 与 .tcr[data-theme="dark"] 的变量里。
// 点击时从按钮中心画圆,半径按视口最远角计算;换回浅色时旧的深色截图收回按钮。
// 不支持 API、减少动态:直接换色,不假装动画。一次只允许一个此类转场,忙碌时 toggle 返回 false。
// 返回 toggle(): Promise<boolean>、destroy()。销毁跳过截图动画;未执行的更新不会在销毁后晚到。
// 数据加载应在调用前完成;此函数不处理请求。View Transitions 会短暂截取页面,不用于卡片墙的自动循环。
const instances = new WeakMap();
let active = null;
export function themeCircleReveal(root, button, {
duration = 600,
easing = 'cubic-bezier(0.4, 0, 0.2, 1)',
} = {}) {
instances.get(root)?.destroy();
let dead = false, busy = false, transition = null, animation = null;
const pressed = button.getAttribute('aria-pressed');
button.setAttribute('aria-pressed', String(root.dataset.theme === 'dark'));
const commit = (dark) => {
if (dead) return false;
root.dataset.theme = dark ? 'dark' : 'light';
button.setAttribute('aria-pressed', String(dark));
return true;
};
const toggle = async () => {
if (dead || busy || active) return false;
const dark = root.dataset.theme !== 'dark';
if (!document.startViewTransition || matchMedia('(prefers-reduced-motion: reduce)').matches) return commit(dark);
busy = true; active = ctl;
const html = document.documentElement;
const oldDirection = html.getAttribute('data-tcr-direction');
html.setAttribute('data-tcr-direction', dark ? 'dark' : 'light');
const r = button.getBoundingClientRect();
const x = r.left + r.width / 2, y = r.top + r.height / 2;
const radius = Math.hypot(Math.max(x, innerWidth - x), Math.max(y, innerHeight - y));
let changed = false;
try {
transition = document.startViewTransition(() => { changed = commit(dark); });
// skipped/销毁时 ready 会拒绝,finished 仍须接住;更新本身由浏览器执行。
try {
await transition.ready;
if (!dead) {
const small = `circle(0px at ${x}px ${y}px)`, large = `circle(${radius}px at ${x}px ${y}px)`;
animation = html.animate({ clipPath: dark ? [small, large] : [large, small] }, {
duration, easing, pseudoElement: dark ? '::view-transition-new(root)' : '::view-transition-old(root)',
});
await animation.finished.catch(() => undefined);
}
} catch { /* 浏览器跳过截图:更新仍会完成,不另造动画。 */ }
await transition.finished;
return changed;
} finally {
busy = false; transition = null; animation = null;
if (active === ctl) {
active = null;
if (oldDirection === null) html.removeAttribute('data-tcr-direction'); else html.setAttribute('data-tcr-direction', oldDirection);
}
}
};
const click = () => { toggle().catch(() => undefined); };
const ctl = { toggle, destroy() {
if (dead) return; dead = true;
button.removeEventListener('click', click); transition?.skipTransition(); animation?.cancel();
if (pressed === null) button.removeAttribute('aria-pressed'); else button.setAttribute('aria-pressed', pressed);
if (instances.get(root) === ctl) instances.delete(root);
} };
instances.set(root, ctl); button.addEventListener('click', click); return ctl;
}HTML
<!-- root 可以是区域,也可以是整页的 html;这里只切换文章区域,不改变站点其他地方。 -->
<section class="tcr" data-theme="light">
<button type="button" aria-label="深色模式" aria-pressed="false">◐</button>
<h2>夜读</h2><p>留一点时间,读完今天这一页。</p>
</section>
<script type="module">
import { themeCircleReveal } from './theme-circle-reveal.js';
const root = document.querySelector('.tcr');
const theme = themeCircleReveal(root, root.querySelector('button'), { duration: 600 });
// 按钮支持鼠标、回车和空格;路由卸载时 theme.destroy()。
</script>