crossfade交叉淡入淡出
用重叠淡入淡出切换等尺寸状态,减少视觉跳变。
预览
一个元素淡出的同时,另一个元素在同一位置淡入。
标准减弱
标准减弱
预览
实现「交叉淡入淡出」:用重叠淡入淡出切换等尺寸状态,减少视觉跳变。 参数设为时长 200ms、重叠 50%、曲线 calm。在状态发生变化时触发一次,并允许重新播放。使用语义化 HTML,CSS 作用域限定在 .motion-demo--crossfade,并挂载随组件提供的原生 JavaScript 交互运行时。为 prefers-reduced-motion 和 .force-reduced-motion 提供等价的低动态方案。
<div class="motion-demo motion-demo--crossfade" data-motion="crossfade" aria-label="交叉淡入淡出 示例">
<div class="motion-replay-scene">
<div class="motion-state-stack"><article class="motion-state motion-state--from">上一状态</article><article class="motion-state motion-state--to">交叉淡入淡出</article></div>
<div class="motion-action-row"><button class="motion-replay-button" type="button" data-motion-replay>重播动效</button></div>
</div>
</div>.motion-demo.motion-demo--crossfade {
box-sizing: border-box;
position: relative;
display: grid;
place-items: center;
width: min(100%, 34rem);
min-height: 15rem;
padding: 2rem;
overflow: hidden;
color: #18181b;
background: #fafafa;
border: 1px solid #e4e4e7;
border-radius: 16px;
isolation: isolate;
}
.motion-demo.motion-demo--crossfade *, .motion-demo.motion-demo--crossfade *::before, .motion-demo.motion-demo--crossfade *::after {
box-sizing: border-box;
}
.motion-demo.motion-demo--crossfade .motion-surface {
position: relative;
display: grid;
gap: 0.65rem;
width: min(100%, 18rem);
padding: 1.25rem;
background: #ffffff;
border: 1px solid #e4e4e7;
border-radius: 16px;
box-shadow: none;
}
.motion-demo.motion-demo--crossfade .motion-surface strong { font-weight: 500; }
.motion-demo.motion-demo--crossfade .motion-line {
display: block;
width: 100%;
height: 0.5rem;
background: #e4e4e7;
border-radius: 999px;
}
.motion-demo.motion-demo--crossfade .motion-line:last-child { width: 62%; }
.motion-demo.motion-demo--crossfade .motion-button {
position: relative;
min-height: 2.75rem;
padding: 0.75rem 1.1rem;
color: white;
font: inherit;
font-weight: 700;
background: #18181b;
border: 0;
border-radius: 999px;
cursor: pointer;
}
.motion-demo.motion-demo--crossfade .motion-button:focus-visible { outline: 3px solid rgb(37 99 235 / 32%); outline-offset: 3px; }
.motion-demo.motion-demo--crossfade .motion-action-row {
display: flex;
justify-content: center;
gap: 0.5rem;
margin-top: 0.85rem;
}
.motion-demo.motion-demo--crossfade .motion-replay-scene {
width: min(100%, 22rem);
display: grid;
place-items: center;
}
.motion-demo.motion-demo--crossfade .motion-pausable-scene {
width: min(100%, 22rem);
display: grid;
place-items: center;
}
.motion-demo.motion-demo--crossfade button.motion-surface {
color: inherit;
font: inherit;
text-align: start;
cursor: pointer;
}
.motion-demo.motion-demo--crossfade .motion-replay-button,
.motion-demo.motion-demo--crossfade .motion-undo-button,
.motion-demo.motion-demo--crossfade .motion-dismiss-button,
.motion-demo.motion-demo--crossfade .motion-pause-button {
min-height: 2.75rem;
padding: 0.65rem 0.9rem;
color: #18181b;
font: 650 0.78rem/1 ui-sans-serif, system-ui, sans-serif;
background: #ffffff;
border: 1px solid #d4d4d8;
border-radius: 8px;
cursor: pointer;
}
.motion-demo.motion-demo--crossfade .motion-replay-button:focus-visible,
.motion-demo.motion-demo--crossfade .motion-undo-button:focus-visible,
.motion-demo.motion-demo--crossfade .motion-dismiss-button:focus-visible,
.motion-demo.motion-demo--crossfade .motion-pause-button:focus-visible,
.motion-demo.motion-demo--crossfade [data-reorder-item]:focus-visible,
.motion-demo.motion-demo--crossfade [data-swipe-target]:focus-visible {
outline: 3px solid rgb(37 99 235 / 32%);
outline-offset: 3px;
}
.motion-demo.motion-demo--crossfade .motion-status {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip: rect(0, 0, 0, 0);
white-space: nowrap;
border: 0;
}
.motion-demo.motion-demo--crossfade .motion-ripple-ink {
position: absolute;
width: 12px;
height: 12px;
pointer-events: none;
background: rgb(255 255 255 / 72%);
border-radius: 50%;
transform: translate(-50%, -50%) scale(0.95);
opacity: 0;
}
.motion-demo.motion-demo--crossfade .motion-state-stack { position: relative; width: min(100%, 18rem); height: 9rem; }
.motion-demo.motion-demo--crossfade .motion-state { position: absolute; inset: 0; display: grid; place-items: center; padding: 1rem; background: #ffffff; border: 1px solid #e4e4e7; border-radius: 16px; }
.motion-demo.motion-demo--crossfade .motion-state--to { background: #18181b; color: white; }
.motion-demo.motion-demo--crossfade .motion-scroll-track > span { color: #71717a; font: 600 0.72rem/1.2 ui-monospace, monospace; letter-spacing: 0.08em; text-transform: uppercase; }
.motion-demo.motion-demo--crossfade .motion-orbit-item { display: grid; place-items: center; width: 2.5rem; height: 2.5rem; color: white; background: #2563eb; border-radius: 50%; }
.motion-demo.motion-demo--crossfade .motion-text-stack { position: relative; width: min(100%, 18rem); min-height: 4rem; }
.motion-demo.motion-demo--crossfade svg { width: min(100%, 20rem); height: auto; }
.motion-demo.motion-demo--crossfade .motion-state-stack { position: relative; width: min(100%, 18rem); height: 9rem; }
.motion-demo.motion-demo--crossfade .motion-state { position: absolute; inset: 0; display: grid; place-items: center; background: #ffffff; border: 1px solid #e4e4e7; border-radius: 16px; }
.motion-demo.motion-demo--crossfade .motion-state--from { animation: motion-crossfade-out 200ms linear both; }
.motion-demo.motion-demo--crossfade .motion-state--to { animation: motion-crossfade-in 200ms linear 100ms both; }
@keyframes motion-crossfade-out { to { opacity: 0; } }
@keyframes motion-crossfade-in { from { opacity: 0; } to { opacity: 1; } }
@keyframes motion-crossfade-reduced-fade {
from { opacity: 0.84; }
to { opacity: 1; }
}
.force-reduced-motion .motion-demo.motion-demo--crossfade {
animation: motion-crossfade-reduced-fade 160ms cubic-bezier(0.23, 1, 0.32, 1) both !important;
}
.force-reduced-motion .motion-demo.motion-demo--crossfade *,
.force-reduced-motion .motion-demo.motion-demo--crossfade *::before,
.force-reduced-motion .motion-demo.motion-demo--crossfade *::after {
animation: none !important;
animation-delay: 0ms !important;
animation-iteration-count: 1 !important;
transition-property: opacity, color, background-color, border-color !important;
transition-duration: 160ms !important;
transition-delay: 0ms !important;
scroll-behavior: auto !important;
}
.force-reduced-motion .motion-demo.motion-demo--crossfade .motion-button,
.force-reduced-motion .motion-demo.motion-demo--crossfade [data-reorder-item],
.force-reduced-motion .motion-demo.motion-demo--crossfade [data-swipe-target],
.force-reduced-motion .motion-demo.motion-demo--crossfade [data-spring-target] {
transform: none !important;
}
.force-reduced-motion .motion-demo.motion-demo--crossfade .motion-path { stroke-dashoffset: 0 !important; }
.force-reduced-motion .motion-demo.motion-demo--crossfade .motion-word--from { opacity: 0 !important; visibility: hidden; }
.force-reduced-motion .motion-demo.motion-demo--crossfade .motion-word:not(.motion-word--from) { opacity: 1 !important; filter: none !important; transform: none !important; }
@media (prefers-reduced-motion: reduce) {
.motion-demo.motion-demo--crossfade {
animation: motion-crossfade-reduced-fade 160ms cubic-bezier(0.23, 1, 0.32, 1) both !important;
}
.motion-demo.motion-demo--crossfade *, .motion-demo.motion-demo--crossfade *::before, .motion-demo.motion-demo--crossfade *::after {
animation: none !important;
animation-delay: 0ms !important;
animation-iteration-count: 1 !important;
transition-property: opacity, color, background-color, border-color !important;
transition-duration: 160ms !important;
transition-delay: 0ms !important;
scroll-behavior: auto !important;
}
.motion-demo.motion-demo--crossfade .motion-button,
.motion-demo.motion-demo--crossfade [data-reorder-item],
.motion-demo.motion-demo--crossfade [data-swipe-target],
.motion-demo.motion-demo--crossfade [data-spring-target] {
transform: none !important;
}
.motion-demo.motion-demo--crossfade .motion-path { stroke-dashoffset: 0 !important; }
.motion-demo.motion-demo--crossfade .motion-word--from { opacity: 0 !important; visibility: hidden; }
.motion-demo.motion-demo--crossfade .motion-word:not(.motion-word--from) { opacity: 1 !important; filter: none !important; transform: none !important; }
}(() => {
const config = {
"id": "crossfade",
"duration": 200,
"distance": 72,
"resistance": 65,
"position": 50,
"stiffness": 220,
"damping": 24,
"mass": 1,
"velocity": 0,
"rippleSize": 220,
"rippleOpacity": 24,
"dragScale": 103,
"autoplay": false
};
const mount = (root, config) => {
const cleanups = [];
const addCleanup = (callback) => cleanups.push(callback);
const cleanup = () => {
for (let index = cleanups.length - 1; index >= 0; index -= 1) {
try { cleanups[index](); } catch { /* keep cleanup reentrant */ }
}
cleanups.length = 0;
};
const on = (target, type, listener, options) => {
target.addEventListener(type, listener, options);
addCleanup(() => target.removeEventListener(type, listener, options));
};
const reduced = () => root.closest(".force-reduced-motion") !== null ||
window.matchMedia("(prefers-reduced-motion: reduce)").matches;
const watchReduced = (callback) => {
const media = window.matchMedia("(prefers-reduced-motion: reduce)");
const notify = () => callback(reduced());
media.addEventListener("change", notify);
const observer = typeof MutationObserver === "undefined" ? null : new MutationObserver(notify);
let ancestor = root;
while (observer && ancestor) {
observer.observe(ancestor, { attributes: true, attributeFilter: ["class"] });
ancestor = ancestor.parentElement;
}
addCleanup(() => {
media.removeEventListener("change", notify);
if (observer) observer.disconnect();
});
};
const replay = root.querySelector("[data-motion-replay]");
if (!replay) return cleanup;
const restart = () => {
root.getAnimations({ subtree: true }).forEach((animation) => {
animation.cancel();
animation.currentTime = 0;
animation.play();
});
};
addCleanup(() => root.getAnimations({ subtree: true }).forEach((animation) => {
if (animation.playState === "paused") animation.play();
}));
on(replay, "click", restart);
return cleanup;
};
document.querySelectorAll("[data-motion=\"crossfade\"]").forEach((root) => {
const cleanupKey = "__motionLexiconCleanup";
if (typeof root[cleanupKey] === "function") root[cleanupKey]();
root[cleanupKey] = mount(root, config);
});
})();深入了解
术语与辨析
crossfade交叉淡入淡出
Crossfade英文定义
Two overlapping states exchange opacity over the same interval, handing visual focus from one to the other.
中文翻译
一个元素淡出的同时,另一个元素在同一位置淡入。
实现前检查
- 目的
- 在同一位置切换尺寸接近的状态,减少内容瞬间替换造成的跳变。
- 频率
- 适合中高频标签、图标和媒体状态;连续输入期间缩短重叠时间。
- 触发
- 由选中项、内容版本或媒体状态变化触发,两个状态共享布局槽位。
- 进入 / 离开
- 旧状态淡出与新状态淡入部分重叠;离开元素在不可见后移除。
- 可打断性
- 快速切换时从当前混合比例继续,只保留最新目标。
- 手势与键盘
- 无需手势;滑块驱动的对比应使用 Before / after slider。
- 减弱动效策略
- 缩短到 80–120ms,或直接替换并保留稳定尺寸。
可访问性
减弱动效
缩短到 80–120ms,或直接替换并保留稳定尺寸。
参数参考
| 参数 | 范围 / 选项 | 默认值 | 作用 |
|---|---|---|---|
duration | 80ms–1200ms | 200ms | 控制一次动效完成所需的时间。 |
overlap | 0%–100% | 50% | 调节重叠。 |
ease | 柔和 / 清脆 / 平稳 / 匀速 | 平稳 | 控制速度随时间变化的方式。 |
交付前检查
- 确保重叠文字不会被辅助技术重复朗读。
- 检查两个状态的容器尺寸不会相互挤压。