中文
北煜ii的作品集
Entrance & exit · Vanilla JS + CSS

Bounce in

过冲弹入

Pop content in with a springy overshoot.

Also searched as: 冲过头再弹回来的出现动画, 弹跳着出现, 按钮弹出来, 提示框弹入

AI Markdown (.md)
Placement
Button, Card list
Mood
Playful, Dramatic
Trigger
进入屏幕
Site types
个人网站, 作品集, 工具站

How it works & accessibility

Scale the element up from smaller with a damped-spring curve sampled into a CSS linear() easing (overshooting cubic-bezier fallback), using the individual scale property; nothing animates with reduced motion.

Best for: Button, Card list (进入屏幕).

Reduced motion: 不播放动画,元素一直是原来的大小、直接看得见

Mobile & touch: 和桌面一样;手机上起始大小靠近 1 一点、冲过头少一点,不显得晃

Key parameters

ParameterKeyDefaultRangeNotes
时长duration700ms——
起始大小from0.6倍——
冲过头多少overshoot15%——

Ready-to-paste AI prompt

Add “Bounce in” to the button in my project: Scale the element up from smaller with a damped-spring curve sampled into a CSS linear() easing (overshooting cubic-bezier fallback), using the individual scale property; nothing animates with reduced motion.
Parameters: duration: 700ms, from: 0.6×, overshoot: 15%.
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/bounce-in.md (canonical specification and reference code).

Dependency-free reference code

CSS
/* 过冲弹入不需要额外的样式:元素默认就是最终的样子,动画开始前那一瞬才由脚本缩小、藏起来 */
JavaScript
// 过冲弹入(Bounce in)· 原生 JS,不依赖任何库。
// 参考 StyleKit 的 bounce-in、Animation Handbook 的 bounce-in 的做法(都是 MIT),自己实现,没有复制代码。
// el 从缩小(from 倍)、透明的样子开始放大,冲过原来的大小一点(overshoot),再弹回来停住、变清楚。返回一个清理函数:调用它会停掉动画,元素回到最终状态。
// 曲线:阻尼弹簧的阶跃响应(按"冲过头多少"反算阻尼,进度 1 时已经稳在 1% 以内),采样成几十个点写进 CSS 的 linear() 当缓动;
// 最大的缩放 ≈ 1 + (1 − from) × overshoot / 100。linear() 要 Chrome 113+、Firefox 112+、Safari 17.2+,不支持时退回
// cubic-bezier(0.34, 1.56, 0.64, 1)(大约冲过头 10% 的近似,不随 overshoot 变)。
// 用 scale 属性做,不碰元素原来的 transform;不写任何行内样式。透明度和缩放用同一条曲线,冲过头时透明度停在 1。
// 开启"减少动态"时不播放,直接显示;动画用 fill: 'backwards',脚本出错或没执行时内容照样可见。
// ignoreReducedMotion 只给"用户主动点播放"的演示用,网站自己的元素不要打开它。
// from 是倍数(0.6 就是从 60% 大小开始),overshoot 写百分数(15 就是冲过头 15%),和数据里同一个数;overshoot 最多按 40 算(再大会把内容弹出屏幕)。
export function bounceIn(el, {
  duration = 700,
  from = 0.6,
  overshoot = 15,
  ignoreReducedMotion = false,
} = {}) {
  if (!ignoreReducedMotion && matchMedia('(prefers-reduced-motion: reduce)').matches) return () => {};
  const animation = el.animate(
    [{ opacity: 0, scale: String(from) }, { opacity: 1, scale: '1' }],
    { duration, easing: springEasing({ overshoot }), fill: 'backwards' },
  );
  return () => animation.cancel();
}

// 元素进入屏幕时播放一次。离屏幕底边还有 40px 时就开始(碰到一点就算):动画的第一帧是藏起来的,等露出一截再开始,
// 会先完整露一下、再消失、再进场;按露出的比例算,比屏幕还高的元素可能永远够不到比例、一直不播。
// root: document 让提前量按元素所在页面自己的视口算,放在 iframe 里也管用。返回一个函数:调用它停止观察(还没进屏幕的就不播了),已经开始播的动画也一起停掉(元素回到最终状态)
export function bounceInOnEnter(el, options) {
  let cancel = null;
  const io = new IntersectionObserver(([entry]) => {
    if (!entry.isIntersecting) return;
    io.disconnect();
    cancel = bounceIn(el, options);
  }, { root: document, threshold: 0, rootMargin: '0px 0px 40px 0px' });
  io.observe(el);
  // 返回的停止函数:还没进屏幕就不再等了;已经开始播的动画也一起停掉(元素回到最终状态)
  return () => { io.disconnect(); cancel?.(); };
}

// ---- 弹簧曲线:同"弹簧过冲回弹"(spring)效果里的曲线,复制过来是为了这个文件单独就能用 ----

// 弹簧缓动的 CSS 写法:支持 linear() 时是采样出来的曲线,不支持时是一条过冲的三次贝塞尔
function springEasing({ overshoot = 15, points = 60 } = {}) {
  const supported = typeof CSS !== 'undefined' && CSS.supports && CSS.supports('animation-timing-function', 'linear(0, 1)');
  if (!supported) return 'cubic-bezier(0.34, 1.56, 0.64, 1)';
  const n = Math.max(2, Math.round(points));
  const stops = [];
  for (let i = 0; i < n; i += 1) {
    const p = i / (n - 1);
    const value = i === 0 ? 0 : i === n - 1 ? 1 : Math.round(springValue(p, { overshoot }) * 1e4) / 1e4;
    stops.push(`${value} ${Math.round(p * 1e4) / 100}%`);
  }
  return `linear(${stops.join(', ')})`;
}

// 弹簧在归一化进度 p(0–1)时的值:0 开始,可能冲过 1,p = 1 时稳在 1
function springValue(p, { overshoot = 15 } = {}) {
  if (p <= 0) return 0;
  if (p >= 1) return 1;
  const O = Math.min(Math.max(overshoot, 0), 40) / 100; // 最多冲过 40%(数据里的范围):再大会把内容弹出屏幕
  if (O === 0) {
    // 临界阻尼:不冲过头,w = 10 时进度 1 的误差 < 0.1%
    const w = 10;
    return 1 - (1 + w * p) * Math.exp(-w * p);
  }
  // 欠阻尼:阻尼比 ζ 由"第一次冲过头的比例"反算(峰值 = 1 + exp(-πζ/√(1-ζ²)) = 1 + O);
  // 角频率取到进度 1 时包络线衰减到 1%,所以 p = 1 时已经稳在 1% 以内
  const ln = Math.log(O);
  const zeta = -ln / Math.sqrt(Math.PI ** 2 + ln ** 2);
  const wn = -Math.log(0.01 * Math.sqrt(1 - zeta ** 2)) / zeta;
  const wd = wn * Math.sqrt(1 - zeta ** 2);
  return 1 - Math.exp(-zeta * wn * p) * (Math.cos(wd * p) + ((zeta * wn) / wd) * Math.sin(wd * p));
}
HTML
<!-- 过冲弹入:给要"蹦"出来的元素调用一次,比如关注按钮、新消息的提示 -->
<button type="button" class="follow" id="follow">+ 关注</button>
<script type="module">
  import { bounceInOnEnter } from './bounce-in.js';
  bounceInOnEnter(document.getElementById('follow'), { duration: 700, from: 0.6, overshoot: 15 });
</script>

More in Entrance & exit

Browse all 86 effects →

Shown once per release · Always available in changelog