用 :active 伪类配合 @keyframes 和 Filter: brightness() 实现按钮点击闪亮效果,推荐亮度1.6、时长250ms、ease-out缓动;快速连点宜改用 js 控制 class 防错乱,IE 可降级 box-shadow 模拟。

按钮点击后加短暂闪亮效果,用 filter: brightness() 配合 @keyframes 是轻量又兼容性好的方案,不需要 JS,纯 css 即可实现。
核心思路:点击瞬间提亮再恢复
利用 :active 伪类触发动画,或配合 animation + 自定义 class(如 .clicked)控制。推荐用 :active,简洁直接,适合即时反馈。
- 亮度值范围:
brightness(1)是原始亮度,brightness(1.5)表示提升 50%,brightness(2)更亮;别超过3,否则容易过曝失真 - 动画时长:建议 150–300ms,太短感知弱,太长像卡顿
- 缓动函数:用
ease-out或ease-in-out,比线性更自然
基础实现(:active + keyframes)
直接在按钮上写:
@keyframes flash-bright { 0% { filter: brightness(1); } 50% { filter: brightness(1.6); } 100% { filter: brightness(1); } } <p>.btn { padding: 8px 16px; border: none; background: #4a6fa5; color: white; cursor: pointer; transition: filter 0.1s; /<em> 让离开时的恢复也平滑 </em>/ }</p><p>.btn:active { animation: flash-bright 0.25s ease-out; }
进阶:避免连续点击错乱
如果用户快速连点,:active 动画可能堆积或中断。稳妥做法是用 JS 添加临时 class,配合 animationend 清理:
立即学习“前端免费学习笔记(深入)”;
- 给按钮绑定
click事件,添加is-flashing类 - CSS 中写
.btn.is-flashing { animation: flash-bright 0.2s ease-out; } - 监听
animationend,自动移除 class,确保下次可再次触发
兼容性与备选提示
filter 在现代浏览器中支持良好(chrome 18+、firefox 35+、safari 9.1+),IE 不支持。如需兼容 IE,可降级为 box-shadow: 0 0 12px rgba(255,255,255,0.8) 模拟发光,但效果略不同。
基本上就这些,不复杂但容易忽略动画时长和亮度梯度,调一调就能很顺滑。