Appearance
点击涟漪
点击涟漪的CSS动画效果,纯CSS实现,无需JavaScript。
实时预览
代码
css
.ripple-btn {
position: relative;
display: inline-block;
padding: 14px 36px;
background: linear-gradient(135deg, #667eea, #764ba2);
color: #ffffff;
font-weight: 600;
border: none;
border-radius: 8px;
cursor: pointer;
overflow: hidden;
-webkit-tap-highlight-color: transparent;
}
.ripple-btn::after {
content: '';
position: absolute;
top: 50%;
left: 50%;
width: 0;
height: 0;
border-radius: 50%;
background: rgba(255, 255, 255, 0.4);
transform: translate(-50%, -50%);
opacity: 0;
transition: width 0s, height 0s, opacity 0s;
}
.ripple-btn:active::after {
width: 300px;
height: 300px;
opacity: 0;
transition: width 0.6s ease, height 0.6s ease, opacity 0.6s ease;
}纯 CSS 实现的点击涟漪悬停效果——当鼠标悬停在元素上时,通过 transition 和 transform 属性触发平滑的视觉变化。不需要任何 JavaScript,拷贝代码即可集成到你的网站中。
快速使用
- 从下方代码块复制 CSS 到你的样式文件中
- 将 HTML 结构粘贴到你的页面模板
- 给目标元素添加对应的 class 名称
- 根据需要调整颜色、大小、动画时长等参数
使用场景
- 卡片和磁贴:悬停时上浮+阴影延伸,给用户可点击的视觉暗示
- 按钮和链接:悬停变色、缩放或发光,提供即时交互反馈
- 图片和缩略图:悬停放大或模糊聚焦,突出重点内容
- 导航菜单:下划线展开或背景色滑入,指示当前位置
常见问题
Q: Hover 效果在手机上不生效怎么办? A: 手机的触摸屏没有 hover 状态。建议用 @media (hover: hover) 包裹 hover 样式,只在支持悬停的设备上应用。对于移动端,可用 :active 伪类做点击反馈。
Q: 动画会影响页面性能吗? A: 本效果只用 transform 和 opacity 做动画,这两个属性由 GPU 合成器处理,不触发页面重排(reflow),性能开销极小,120fps 流畅运行。
Q: 如何修改动画的触发方式? A: 将 :hover 改为 :active(点击触发)、.active(JS 控制类名触发)或配合 @media (prefers-reduced-motion) 做无障碍适配。
浏览器兼容性
本效果使用标准 CSS 属性,所有现代浏览器(Chrome、Firefox、Safari、Edge)均完全支持。
| 浏览器 | 兼容性 |
|---|---|
| Chrome / Edge | ✅ 完全支持 |
| Firefox | ✅ 完全支持 |
| Safari | ✅ 完全支持 |
| 移动端浏览器 | ✅ 完全支持 |
| IE 11 | ⚠️ 部分属性需要 -ms- 前缀或降级 |
实现原理
- A
::afterpseudo-element starts as a zero-size circle, centered withtransform: translate(-50%, -50%) - On
:active(mouse button down), the circle expands to 300px while fading out - The
opacity: 0on the active state creates the fade-out ripple effect overflow: hiddenclips the ripple to the button's border radius- When released, the transition resets instantly (
0s), ready for the next click - For exact click-position ripples, you would need JavaScript to set the origin point