春天去了一趟海边
Fade in up
淡入上移
Fade content in as it moves gently upward.
Also searched as: 滚动显现, 往下滑时内容一块块浮上来, 内容从下面淡淡地浮上来, 滚到哪里,哪里的内容就浮现出来
- Placement
- Article, Card list, Image
- Mood
- Soft, Crisp
- Trigger
- 进入屏幕
- Site types
- 个人网站, 博客, 作品集
How it works & accessibility
Animate opacity from zero to one and translateY from a positive offset to zero.
Best for: Article, Card list, Image (进入屏幕).
Reduced motion: 不播放动画,内容一直在原位、直接看得见
Mobile & touch: 和桌面一样;手机上一屏放得少、滑得快,上移距离可以小一点
Key parameters
| Parameter | Key | Default | Range | Notes |
|---|---|---|---|---|
| 时长 | duration | 800ms | — | — |
| 上移距离 | distance | 30px | — | — |
| 缓动 | easing | cubic-bezier(0.33, 1, 0.68, 1) | — | — |
Ready-to-paste AI prompt
Add “Fade in up” to the article in my project: Animate opacity from zero to one and translateY from a positive offset to zero.
Parameters: duration: 800ms, distance: 30px, easing: cubic-bezier(0.33, 1, 0.68, 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/fade-in-up.md (canonical specification and reference code).Dependency-free reference code
CSS
/* 淡入上移不需要额外的样式:元素默认就是最终的样子,动画开始前那一瞬才由脚本藏起来 */JavaScript
// 淡入上移(Fade in up)· 原生 JS,不依赖任何库。
// 改写自 StyleKit 的 fade-in-up(MIT License,Copyright (c) 2026 AnxForever):动作来自它(从下方一小段、透明开始,往上回到原位)。
// 默认值调过(原版 600ms、20px、很急的 ease-out):滚动显现在露出来之前就开始,曲线太急的话进到屏幕里时已经播完大半,看不出浮上来。
// el 从往下一小段、透明的样子开始,往上移回原位、变清楚。返回一个清理函数:调用它会停掉动画,元素回到原位。
// 用 translate 属性移动,不碰元素原来的 transform(比如卡片本来就歪着放);元素本来就有 translate(比如用它居中)时,在它的基础上往下挪。
// 开启"减少动态"时不播放,直接显示;动画用 fill: 'backwards',脚本出错或没执行时内容照样可见(不要先用 CSS 把内容藏起来,再等脚本显示)。
// ignoreReducedMotion 只给"用户主动点播放"的演示用,网站自己的元素不要打开它。
export function fadeInUp(el, {
duration = 800,
distance = 30,
easing = 'cubic-bezier(0.33, 1, 0.68, 1)',
ignoreReducedMotion = false,
} = {}) {
if (!ignoreReducedMotion && matchMedia('(prefers-reduced-motion: reduce)').matches) return () => {};
// 元素本来的 translate("none" 或 "x y z",每一项可能是 calc(...)、里面还套着 min(...)):按最外层的空格拆开,
// 起点在它下面 distance,终点就是它本来的位置
const base = getComputedStyle(el).translate;
const parts = [];
let depth = 0;
for (const ch of base === 'none' ? '' : base) {
if (ch === ' ' && depth === 0) { parts.push(''); continue; }
if (ch === '(') depth += 1;
if (ch === ')') depth -= 1;
if (!parts.length) parts.push('');
parts[parts.length - 1] += ch;
}
const [x = '0px', y = '0px', z = ''] = parts;
const animation = el.animate(
[{ opacity: 0, translate: `${x} calc(${y} + ${distance}px) ${z}`.trim() }, { opacity: 1, translate: `${x} ${y} ${z}`.trim() }],
{ duration, easing, fill: 'backwards' },
);
return () => animation.cancel();
}
// 一个或一组元素(比如文章里的每一段、每张图):各自快进入屏幕时播放一次,往下滑时一块块浮上来。
// 离屏幕底边还有 40px 时就开始(碰到一点就算):动画的第一帧是藏起来的,等露出一截再开始,会先完整露一下、再消失、再进场;
// 按露出的比例算,比屏幕还高的元素可能永远够不到比例、一直不播。
// 调用时已经在屏幕里的(首屏)马上播:等观察器报告要晚一帧,那一帧它已经完整露过了。已经滚过去的(在屏幕上面)不播;
// 往回滚、从屏幕上边进来的也不播:它一直看得见,不用再藏起来演一遍。
// root 是滚动的那个框,默认整个页面(document 让提前量按元素所在页面自己的视口算,放在 iframe 里也管用)。
/**
* @param {Element | Iterable<Element>} els
* @param {{ root?: Document | Element, duration?: number, distance?: number, easing?: string, ignoreReducedMotion?: boolean }} [options]
*/
export function fadeInUpOnEnter(els, { root = document, ...options } = {}) {
const list = els instanceof Element ? [els] : [...els];
const io = new IntersectionObserver((entries) => {
entries.forEach((entry) => {
if (!entry.isIntersecting) return;
io.unobserve(entry.target);
if (entry.boundingClientRect.top >= (entry.rootBounds?.top ?? 0)) fadeInUp(entry.target, options);
});
}, { root, threshold: 0, rootMargin: '0px 0px 40px 0px' });
const view = root === document ? { top: 0, bottom: innerHeight } : root.getBoundingClientRect();
list.forEach((el) => {
const r = el.getBoundingClientRect();
if (r.bottom <= view.top) return;
if (r.top < view.bottom) fadeInUp(el, options);
else io.observe(el);
});
return () => io.disconnect();
}HTML
<!-- 淡入上移:把要一块块浮上来的元素交给它(比如文章里的每一段、每张图),各自快进入屏幕时出现 -->
<article class="post">
<h2>春天去了一趟海边</h2>
<p>第一段……</p>
<img src="sea.jpg" alt="傍晚的海边">
<p>第二段……</p>
</article>
<script type="module">
import { fadeInUpOnEnter } from './fade-in-up.js';
fadeInUpOnEnter(document.querySelectorAll('.post > *'));
</script>