EN
春天去了一趟海边
入场与退场 · 原生 JS + CSS

淡入上移

Fade in up

往下滑时,内容一块块从下面淡淡地浮上来

常见大白话搜法:滚动显现、往下滑时内容一块块浮上来、内容从下面淡淡地浮上来、滚到哪里,哪里的内容就浮现出来

AI 专用 Markdown (.md)
适用位置
正文、卡片列表、图片
气质感觉
柔和、利落
触发方式
进入屏幕
适合网站
个人网站、博客、作品集

实现原理与无障碍底线

元素快进入屏幕时,从往下一小段、透明的样子开始,往上移回原位,同时变清楚。

适用场景:文章的段落和配图、作品列表、"关于我"的几块内容:往下滑时一块块出现,页面有节奏,又不抢内容的风头。一组卡片想一个接一个出现,用错峰淡入上移

减少动态(prefers-reduced-motion):不播放动画,内容一直在原位、直接看得见

移动端与触屏:和桌面一样;手机上一屏放得少、滑得快,上移距离可以小一点

关键可调参数

参数键名默认值建议范围说明
时长duration800ms——
上移距离distance30px——
缓动easingcubic-bezier(0.33, 1, 0.68, 1)——

可直接复制给 AI(Cursor / Claude / v0)的提示词

请在我的项目里给正文加上「淡入上移」(Fade in up)效果:元素快进入屏幕时,从往下一小段、透明的样子开始,往上移回原位,同时变清楚。
参数:时长 800ms,上移距离 30px,缓动 cubic-bezier(0.33, 1, 0.68, 1)。
要求:保留原有的内容和布局;系统开启"减少动态"时直接显示最终状态;脚本出错或没有执行时内容仍然可见。
参考:https://donglang.linter.top/effects/fade-in-up.md(原理、参数和参考代码)

原生无依赖参考代码

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>

同属「入场与退场」的其他动效

查看全部 86 个动效 →

本次更新仅弹窗提醒一次 · 可随时在历史公告回看