中文
Click & feedback · CSS only

Press feedback

按下缩小回弹

Shrink a button slightly while pressed and spring it back on release.

Also searched as: 按下按钮时缩小一点,松开弹回来, 点按钮的手感反馈, 按下去有按压感的按钮, 按住缩一下,松手回弹

AI Markdown (.md)
Placement
Button
Mood
Crisp, Playful
Trigger
点击
Site types
个人网站, 作品集, 工具站

How it works & accessibility

Scale the button with the individual scale property on :active (or an is-pressed class) with a quick ease-out, and return with an overshooting easing, leaving its transform alone; touch-action: manipulation removes the tap delay, and reduced motion darkens the button instead of scaling.

Best for: Button (点击).

Reduced motion: 不缩放,按下时按钮稍微变暗,松手变回来

Mobile & touch: 手机上靠 :active,按下就缩;touch-action: manipulation 去掉点按的延迟

Key parameters

ParameterKeyDefaultRangeNotes
按下时缩到多少scale0.95倍——
缩下去用多久press90ms——
松手弹回用多久release320ms——
松手弹回的缓动easingcubic-bezier(0.34, 1.56, 0.64, 1)——

Ready-to-paste AI prompt

Add “Press feedback” to the button in my project: Scale the button with the individual scale property on :active (or an is-pressed class) with a quick ease-out, and return with an overshooting easing, leaving its transform alone; touch-action: manipulation removes the tap delay, and reduced motion darkens the button instead of scaling.
Parameters: scale: 0.95×, press: 90ms, release: 320ms, easing: cubic-bezier(0.34, 1.56, 0.64, 1).
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/press-feedback.md (canonical specification and reference code).

Dependency-free reference code

CSS
/* 按下缩小回弹(Press feedback)· 只用 CSS,不需要脚本。参考 StyleKit 的 elastic-scale、Animation Handbook 的 button-press-scale、
   Motion Lexicon 的 press-tap-feedback 的做法(都是 MIT),自己实现,没有复制代码。
   用法:给按钮加 class="pf-btn"。按下(:active)时缩小,松手弹回来;自己画的控件、没法用 :active 的地方,按下时加 class="is-pressed"、松手时去掉。
   - --pf-scale:按下时缩到多少(0.95 就是 95%);--pf-press:缩下去用多久;--pf-release:松手弹回用多久;
     --pf-easing:松手弹回的缓动(默认会冲过 1 一点点再回来,像弹了一下)。
   - 用 scale 属性做,不碰按钮原来的 transform。
   - touch-action: manipulation:去掉手机上点按的延迟(双击缩放的等待),手机上按下就靠 :active。
   - 过渡写在 .pf-btn 上(不在 :where() 里):网站自己在 .pf-btn 上再写 transition 简写(比如 color .2s)会盖掉缩放的过渡,想要别的过渡就把 scale 的那一段一起写进去。
   - 键盘:按住空格时有按下的样子(:active);回车是一按就触发 click、没有"按住",浏览器不给 :active,只用 CSS 做不出来
     (要的话自己加几行脚本:keydown 回车时给按钮加 is-pressed,keyup 或 click 以后去掉)。
   - 开启"减少动态"时:不缩放,按下时稍微变暗(filter: brightness(0.92)),松手变回来。 */
/* 默认值写在 :where() 里(不占优先级):网站自己在 .pf-btn 上改的变量,不管写在这段代码前面还是后面,都用网站自己的 */
:where(.pf-btn) {
  --pf-scale: 0.95;
  --pf-press: 90ms;
  --pf-release: 320ms;
  --pf-easing: cubic-bezier(0.34, 1.56, 0.64, 1);
}
.pf-btn {
  transition: scale var(--pf-release) var(--pf-easing);
  touch-action: manipulation;
}
.pf-btn:active,
.pf-btn.is-pressed {
  scale: var(--pf-scale);
  transition-duration: var(--pf-press);
  transition-timing-function: ease-out;
}
@media (prefers-reduced-motion: reduce) {
  .pf-btn {
    transition: filter 90ms ease-out;
  }
  .pf-btn:active,
  .pf-btn.is-pressed {
    scale: none;
    filter: brightness(0.92);
  }
}
HTML
<!-- 按下缩小回弹:给按钮加 class="pf-btn" 就行,不需要脚本。四个 CSS 变量可以改手感 -->
<button type="button" class="pf-btn" style="--pf-scale: 0.95; --pf-press: 90ms; --pf-release: 320ms;">提交</button>

More in Click & feedback

Browse all 86 effects →

Shown once per release · Always available in changelog