Shake
左右抖动提示
Shake an element side to side to signal an error.
Also searched as: 输错了让按钮左右抖一下, 不能点的时候左右晃动提醒, 表单出错抖动提示, 像摇头一样左右抖
- Placement
- Button
- Mood
- Crisp, Dramatic
- Trigger
- 点击
- Site types
- 个人网站, 工具站
How it works & accessibility
Animate the individual translate property additively through decaying left-right keyframes (ease-in-out per segment) without touching transform, cancelling any previous shake; pair it with role=alert error text and aria-invalid, and with reduced motion flash an outline instead.
Best for: Button (点击).
Reduced motion: 不抖动,改成元素外圈闪一下红色的描边;错误文字照常显示
Mobile & touch: 和桌面一样;手机上抖的距离小一点(6–8px),免得像页面坏了
Key parameters
| Parameter | Key | Default | Range | Notes |
|---|---|---|---|---|
| 抖多远 | distance | 10px | — | — |
| 抖多久 | duration | 500ms | — | — |
Ready-to-paste AI prompt
Add “Shake” to the button in my project: Animate the individual translate property additively through decaying left-right keyframes (ease-in-out per segment) without touching transform, cancelling any previous shake; pair it with role=alert error text and aria-invalid, and with reduced motion flash an outline instead.
Parameters: distance: 10px, duration: 500ms.
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/shake.md (canonical specification and reference code).Dependency-free reference code
CSS
/* 左右抖动提示不需要额外的样式:抖动由 shake.js 用 element.animate 叠加在元素的 translate 上。
错误文字的样式由页面自己写(structure.html 里的 .form-error 只是例子)。 */JavaScript
// 左右抖动提示(Shake)· 原生 JS,不依赖任何库。
// 参考 StyleKit 的 shake、Animation Handbook 的 error-shake、Motion Lexicon 的 shake-wiggle 的做法(都是 MIT),自己实现,没有复制代码。
// 用法:提交失败时 shake(按钮或输入框),返回这次的 Animation。选项:distance 抖多远(px,默认 10),duration 抖多久(毫秒,默认 500)。
// 动法:在 translate 属性上做几段左右来回,越抖越小:位置 0、0.15、0.35、0.55、0.75、0.9、1 对应横向位移
// 0、-distance、+distance、-0.6×distance、+0.6×distance、-0.2×distance、0;每一段 ease-in-out,整个动画 linear;
// composite: 'add' 叠加在元素原来的 translate 上(比如用 translate 居中的元素照样抖),不碰 transform;抖完回到原位,不留行内样式。
// 又抖一次(上一次还没抖完):先停掉上一次再开始,每个元素同时只有一个抖动。
// 只抖不够:抖动不能是出错的唯一提示——色觉不同、开了减少动态、用读屏的人都看不到它。要同时显示错误文字(role="alert" 或 aria-live),
// 出错的输入框加 aria-invalid="true"(structure.html 里有完整的例子);没有脚本时错误文字由页面自己决定怎么显示。
// 开启"减少动态"时不位移:改成元素外圈闪一下红色描边(outline,600ms 淡出),返回值的类型不变(还是 Animation);
// ignoreReducedMotion 只给"用户主动点播放"的演示用。没有监听器、没有定时器,所以没有 destroy()。
// 每个元素现在的抖动(又抖一次时先停掉它)
const running = new WeakMap();
export function shake(el, {
distance = 10,
duration = 500,
ignoreReducedMotion = false,
} = {}) {
running.get(el)?.cancel();
let animation;
if (!ignoreReducedMotion && matchMedia('(prefers-reduced-motion: reduce)').matches) {
animation = el.animate(
[
{ outlineStyle: 'solid', outlineWidth: '2px', outlineOffset: '2px', outlineColor: '#e5484d' },
{ outlineStyle: 'solid', outlineWidth: '2px', outlineOffset: '2px', outlineColor: 'rgb(229 72 77 / 0)' },
],
{ duration: 600, easing: 'ease-out' },
);
} else {
const offsets = [0, 0.15, 0.35, 0.55, 0.75, 0.9, 1];
const xs = [0, -1, 1, -0.6, 0.6, -0.2, 0];
animation = el.animate(
offsets.map((offset, k) => ({ offset, translate: `${xs[k] * distance}px 0px`, easing: 'ease-in-out' })),
{ duration, easing: 'linear', composite: 'add' },
);
}
running.set(el, animation);
const forget = () => { if (running.get(el) === animation) running.delete(el); };
animation.finished.then(forget, forget);
return animation;
}HTML
<!-- 左右抖动提示:提交失败时抖按钮,同时显示错误文字(role="alert")、给输入框加 aria-invalid——抖动不能是唯一的提示 -->
<form id="login" novalidate>
<label for="password">密码</label>
<input id="password" type="password" aria-describedby="password-error">
<button type="submit" id="login-button">登录</button>
<p class="form-error" id="password-error" role="alert"></p>
</form>
<script type="module">
import { shake } from './shake.js';
const form = document.getElementById('login');
const input = document.getElementById('password');
const error = document.getElementById('password-error');
form.addEventListener('submit', (event) => {
event.preventDefault();
if (input.value === '') {
input.setAttribute('aria-invalid', 'true');
// 同一句错误连续出现两次:和现在的字一样时在句尾加一个零宽空格,读屏才会再念一次
const message = '请先填密码';
error.textContent = error.textContent === message ? `${message}\u200b` : message;
shake(document.getElementById('login-button'), { distance: 10, duration: 500 });
}
});
</script>