Scale in
缩放淡入
Scale content up from slightly smaller as it fades in.
Also searched as: 内容从小一点慢慢放大淡入, 卡片缩放着出现, 配图由小变大淡入, 弹层放大着出现
- Placement
- Card list, Image
- Mood
- Soft, Crisp
- Trigger
- 进入屏幕
- Site types
- 个人网站, 作品集, 博客
How it works & accessibility
Animate the individual scale property and opacity just before the element enters the viewport, leaving its own transform untouched; content stays visible without JavaScript, and nothing animates with reduced motion.
Best for: Card list, Image (进入屏幕).
Reduced motion: 不播放动画,内容一直是原来的大小、直接看得见
Mobile & touch: 和桌面一样;手机上一屏放得少,起始大小可以靠近 1 一点(比如 0.95),不显得晃
Key parameters
| Parameter | Key | Default | Range | Notes |
|---|---|---|---|---|
| 时长 | duration | 500ms | — | — |
| 起始大小 | scale | 0.9倍 | — | — |
| 缓动 | easing | cubic-bezier(0.16, 1, 0.3, 1) | — | — |
Ready-to-paste AI prompt
Add “Scale in” to the card list in my project: Animate the individual scale property and opacity just before the element enters the viewport, leaving its own transform untouched; content stays visible without JavaScript, and nothing animates with reduced motion.
Parameters: duration: 500ms, scale: 0.9×, easing: cubic-bezier(0.16, 1, 0.3, 1).
Preserve existing content and layout. Respect prefers-reduced-motion by showing the final state. Content must remain visible if JavaScript fails or is disabled.
Reference: https://donglang.linter.top/effects/scale-in.md (canonical specification and reference code).Dependency-free reference code
CSS
/* 缩放淡入不需要额外的样式:元素默认就是最终的样子,动画开始前那一瞬才由脚本缩小、藏起来 */JavaScript
// 缩放淡入(Scale in)· 原生 JS,不依赖任何库。
// 参考 StyleKit 的 scale-in、Animation Handbook 的 scale-in、Motion Lexicon 的 scale-in 的做法(都是 MIT),自己实现,没有复制代码。
// el 从缩小一点(scale 倍)、透明的样子开始,放大回原来的大小、变清楚。返回一个清理函数:调用它会停掉动画,元素回到最终状态。
// 用 scale 属性做,不碰元素原来的 transform(比如卡片本来就歪着放 rotate(3deg));不写任何行内样式。
// 开启"减少动态"时不播放,直接显示;动画用 fill: 'backwards',脚本出错或没执行时内容照样可见(不要先用 CSS 把内容藏起来,再等脚本显示)。
// ignoreReducedMotion 只给"用户主动点播放"的演示用,网站自己的元素不要打开它。
// scale 是倍数(0.9 就是从 90% 大小开始),和数据里"倍"同一个数。
export function scaleIn(el, {
duration = 500,
scale = 0.9,
easing = 'cubic-bezier(0.16, 1, 0.3, 1)',
ignoreReducedMotion = false,
} = {}) {
if (!ignoreReducedMotion && matchMedia('(prefers-reduced-motion: reduce)').matches) return () => {};
const animation = el.animate(
[{ opacity: 0, scale: String(scale) }, { opacity: 1, scale: '1' }],
{ duration, easing, fill: 'backwards' },
);
return () => animation.cancel();
}
// 元素进入屏幕时播放一次。
// 离屏幕底边还有 40px 时就开始(碰到一点就算):动画的第一帧是藏起来的,等露出一截再开始,会先完整露一下、再消失、再进场;
// 按露出的比例算,比屏幕还高的元素可能永远够不到比例、一直不播。root: document 让提前量按元素所在页面自己的视口算,放在 iframe 里也管用。
// 返回一个函数:调用它停止观察(还没进屏幕的就不播了),已经开始播的动画也一起停掉(元素回到最终状态)
export function scaleInOnEnter(el, options) {
let cancel = null;
const io = new IntersectionObserver(([entry]) => {
if (!entry.isIntersecting) return;
io.disconnect();
cancel = scaleIn(el, options);
}, { root: document, threshold: 0, rootMargin: '0px 0px 40px 0px' });
io.observe(el);
// 返回的停止函数:还没进屏幕就不再等了;已经开始播的动画也一起停掉(元素回到最终状态)
return () => { io.disconnect(); cancel?.(); };
}HTML
<!-- 缩放淡入:给要出现的元素调用一次,比如作品列表里的每张卡片 -->
<article class="work" id="work">
<img src="cover.jpg" alt="作品封面">
<h3>海边的周末</h3>
</article>
<script type="module">
import { scaleInOnEnter } from './scale-in.js';
scaleInOnEnter(document.getElementById('work'), { duration: 500, scale: 0.9 });
</script>