中文
已保存
Click & feedback · Vanilla JS + CSS

Spring

弹簧过冲回弹

Drop something in with a springy overshoot.

Also searched as: 冲过头再弹回来, 弹性回弹, 像弹簧一样弹一下, 带回弹的出现动画

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

How it works & accessibility

Sample a real damped-spring step response into a CSS linear() easing, with an overshooting cubic-bezier fallback; with reduced motion the element jumps to its final state.

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

Reduced motion: 不播放动画,东西直接停在最后的位置

Mobile & touch: 和桌面一样;手机上位移别太大,冲过头的比例调小一点更稳

Key parameters

ParameterKeyDefaultRangeNotes
稳下来的时间duration700ms——
冲过头多少overshoot15%——

Ready-to-paste AI prompt

Add “Spring” to the button in my project: Sample a real damped-spring step response into a CSS linear() easing, with an overshooting cubic-bezier fallback; with reduced motion the element jumps to its final state.
Parameters: duration: 700ms, 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/spring.md (canonical specification and reference code).

Dependency-free reference code

CSS
/* 弹簧过冲回弹不需要额外的样式:动画和缓动都由 spring.js 里的 springTo / springEasing 写进 element.animate。
   只想用 CSS 的话,把 springEasing() 返回的 linear(...) 抄进自己的 transition-timing-function 或 animation-timing-function 就行。 */
JavaScript
// 弹簧过冲回弹(Spring)· 原生 JS,不依赖任何库。
// 参考 StyleKit 的 elastic-snap、Motion Lexicon 的 spring 的做法(都是 MIT),自己实现,没有复制代码:东西冲过目标位置一点点,再弹回来停住。
// 曲线是标准的二阶阻尼弹簧的阶跃响应:按"冲过头多少(overshoot)"反算阻尼比,在进度 1 的时候已经稳在 1% 以内,
// 再把曲线采样成几十个点,写进 CSS 的 linear() 当缓动——元素的位移、缩放、透明度都能用同一条曲线。
// 用法:springTo(元素, [起点关键帧, 终点关键帧], { duration, overshoot }),和 element.animate 一样,返回那个 Animation。
// 只想要缓动:springEasing({ overshoot }) 返回一个 CSS 缓动字符串,可以写进自己的 animate / transition。
// 浏览器支持:linear() 要 Chrome 113+、Firefox 112+、Safari 17.2+。不支持时 springEasing 退回 cubic-bezier(0.34, 1.56, 0.64, 1)
// (大约冲过头 10% 的三次贝塞尔,是一条够用的近似,不会随 overshoot 变)。
// overshoot 在 options 里写百分数(15 就是冲过目标 15%),和数据里 % 同一个数,范围 0–40(写得再大也按 40 算,免得把内容弹出屏幕);0 是不冲过头、但仍然是"快到慢"的弹簧式收尾。
// 同一个元素又调一次 springTo:上一次返回的动画还留着(fill: 'both'),要换成新的,调用方先对上一次返回的动画调 cancel(),不然取消新的以后上一次的样子会露出来。
// duration 只决定"稳下来"要多久(毫秒);曲线本身用归一化的进度 0–1,换 duration 不改形状。
// 开启"减少动态"时 springTo 不播:用 0 时长的动画直接到最后一个关键帧并 finish(),返回值的类型不变(还是 Animation);
// ignoreReducedMotion 只给"用户主动点播放"的演示用,网站自己的元素不要打开它。
// 没有监听器、没有定时器,所以没有 destroy()。

// 让元素按弹簧曲线动起来:keyframes 同 element.animate(至少起点和终点),返回 Animation;动画结束后停在最后一个关键帧(fill: 'both')
export function springTo(element, keyframes, { duration = 700, overshoot = 15, ignoreReducedMotion = false } = {}) {
  if (!ignoreReducedMotion && matchMedia('(prefers-reduced-motion: reduce)').matches) {
    const jump = element.animate(keyframes, { duration: 0, fill: 'both' });
    jump.finish();
    return jump;
  }
  return element.animate(keyframes, { duration, easing: springEasing({ overshoot }), fill: 'both' });
}

// 弹簧缓动的 CSS 写法:支持 linear() 时是采样出来的曲线,不支持时是一条过冲的三次贝塞尔
export 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
export 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
<!-- 弹簧过冲回弹:要弹的东西就是一个普通元素(这里是提示条),位置的起点和终点由你的关键帧决定,缓动由 springTo 给 -->
<div class="toast" id="toast">已保存 ✓</div>
<script type="module">
  import { springTo } from './spring.js';
  const toast = document.getElementById('toast');
  // 从上面 80px、小一点,落到原位,冲过头 15% 再弹回来,700ms 稳住
  springTo(
    toast,
    [{ translate: '0 -80px', scale: 0.92 }, { translate: '0 0', scale: 1 }],
    { duration: 700, overshoot: 15 },
  );
</script>

More in Click & feedback

Browse all 86 effects →

Shown once per release · Always available in changelog