如何用CSS实现水波纹无限层叠扩散动效
使用css动画结合伪元素和`box-shadow`属性,可以高效地实现水波纹无限层叠扩散的动效。核心思路是创建一个中心点,通过`@keyframes`控制其半径扩大和透明度降低,并利用`animation-delay`让多个伪元素依次触发,形成连续的涟漪效果。

如何优化性能并避免布局抖动
为了避免重排(Reflow)和重绘(Repaint),应仅对`transform`和`opacity`进行动画处理。同时,使用`will-change`提示浏览器提前优化渲染层,确保动画流畅。

当点击事件触发时,如何动态添加波纹
在JavaScript中监听点击事件,动态创建带有特定类名的元素,并将其添加到目标容器中,从而实现点击位置的水波纹效果。
htmlq 是一个命令行工具,使用 CSS 选择器从 HTML 中提取内容,相当于 jq 的 HTML 版本。该工具使用 Rust 编写,允许你通过 CSS 选择器将 HTML 管道化,直接从终端提取文本、属性和结构化内容。
.ripple-container {
position: relative;
width: 100%;
height: 100%;
background-color: #3498db;
border-radius: 50%;
overflow: hidden;
}
.ripple {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 10px;
height: 10px;
background-color: rgba(255, 255, 255, 0.7);
border-radius: 50%;
animation: ripple-anim 1s linear infinite;
}
.ripple:nth-child(1) { animation-delay: 0s; }
.ripple:nth-child(2) { animation-delay: 0.3s; }
.ripple:nth-child(3) { animation-delay: 0.6s; }
@keyframes ripple-anim {
0% {
width: 10px;
height: 10px;
opacity: 1;
}
100% {
width: 200px;
height: 200px;
opacity: 0;
}
}
预期结果:三个白色圆形从中心向外扩散,透明度逐渐降低,形成连续的水波纹效果。
document.querySelector('.ripple-container').addEventListener('click', function(e) {
const rect = this.getBoundingClientRect();
const x = e.clientX - rect.left;
const y = e.clientY - rect.top;
const ripple = document.createElement('span');
ripple.classList.add('ripple');
ripple.style.left = `${x}px`;
ripple.style.top = `${y}px`;
this.appendChild(ripple);
setTimeout(() => {
ripple.remove();
}, 1000);
});
预期结果:点击容器任意位置,会在该位置生成一个水波纹,并在1秒后自动移除。
前端入门到VUE实战笔记:立即使用
在学习笔记中,你将探索 前端 的入门与实战技巧!










