要给按钮上的形状背景添加涟漪效果,可以使用CSS的伪元素和动画效果来实现。以下是一个示例代码:
HTML部分:
CSS部分:
.ripple-button {
position: relative;
overflow: hidden;
border: none;
outline: none;
background-color: #2196F3;
color: #fff;
padding: 12px 24px;
cursor: pointer;
}
.ripple-button:before {
content: "";
position: absolute;
border-radius: 50%;
background-color: rgba(255, 255, 255, 0.7);
width: 100px;
height: 100px;
top: -50px;
left: -50px;
opacity: 0;
z-index: -1;
transform: scale(0);
pointer-events: none;
transition: transform 0.3s ease, opacity 0.3s ease;
}
.ripple-button:hover:before {
opacity: 1;
transform: scale(2);
}
在上面的代码中,我们使用了伪元素.ripple-button:before来创建一个覆盖在按钮上的圆形背景。通过设置opacity为0,使其初始时不可见。当鼠标悬停在按钮上时,我们使用CSS的hover伪类来控制涟漪效果的显示。通过改变opacity和transform的值,实现了背景的淡入淡出和放大效果。
你可以根据需要调整涟漪效果的大小、颜色和动画速度。
上一篇:按钮上的响应式文本和图标