EN
点击与反馈 · 原生 JS + CSS

左右抖动提示

Shake

输错了或者不能点时,按钮左右抖几下

常见大白话搜法:输错了让按钮左右抖一下、不能点的时候左右晃动提醒、表单出错抖动提示、像摇头一样左右抖

AI 专用 Markdown (.md)
适用位置
按钮
气质感觉
利落、戏剧化
触发方式
点击
适合网站
个人网站、工具站

实现原理与无障碍底线

用 element.animate 在 translate 属性上做几段左右来回(越抖越小,每段 ease-in-out),叠加在元素原来的位置上,不碰 transform;又抖一次时先停掉上一次;抖动不能是唯一的提示,要配 role="alert" 的错误文字和 aria-invalid;减少动态时不抖,改成边框外圈闪一下红色。

适用场景:登录密码错了、表单没填完就提交、按钮暂时不能点:让按钮或输入框左右抖几下,一眼就知道"不对"。一定要同时显示错误文字,抖动只是加强

减少动态(prefers-reduced-motion):不抖动,改成元素外圈闪一下红色的描边;错误文字照常显示

移动端与触屏:和桌面一样;手机上抖的距离小一点(6–8px),免得像页面坏了

关键可调参数

参数键名默认值建议范围说明
抖多远distance10px——
抖多久duration500ms——

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

请在我的项目里给按钮加上「左右抖动提示」(Shake)效果:用 element.animate 在 translate 属性上做几段左右来回(越抖越小,每段 ease-in-out),叠加在元素原来的位置上,不碰 transform;又抖一次时先停掉上一次;抖动不能是唯一的提示,要配 role="alert" 的错误文字和 aria-invalid;减少动态时不抖,改成边框外圈闪一下红色。
参数:抖多远 10px,抖多久 500ms。
要求:保留原有的内容和布局;系统开启"减少动态"时直接显示最终状态;脚本出错或没有执行时内容仍然可见。
参考:https://donglang.linter.top/effects/shake.md(原理、参数和参考代码)

原生无依赖参考代码

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>

同属「点击与反馈」的其他动效

查看全部 86 个动效 →

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