CSS3实现一个粒子动效的按钮
以下是一个使用CSS3实现的粒子动效按钮的示例代码:
HTML:
<div class="particles-button">
<button class="btn">点击我</button>
</div>
CSS:
.particles-button {
position: relative;
width: 150px;
height: 50px;
}
.btn {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: none;
border: none;
color: #fff;
font-size: 1.2rem;
cursor: pointer;
padding: 0;
z-index: 10;
}
.particles-button:after {
content: '';
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: linear-gradient(45deg, #ff6e7f, #bfe9ff);
z-index: 1;
transition: opacity 0.5s ease;
opacity: 0;
}
.particles-button:before {
content: '';
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: transparent;
z-index: 2;
transition: background-size 0.5s ease;
background-image: radial-gradient(circle at center, #fff 0%, rgba(255, 255, 255, 0) 70%), radial-gradient(circle at center, #fff 0%, rgba(255, 255, 255, 0) 70%);
background-size: 10% 10%, 20% 20%;
background-position: 0 0, 10px 10px;
}
.btn:hover + .particles-button:after {
opacity: 1;
}
.btn:hover + .particles-button:before {
background-size: 50% 50%, 100% 100%;
}
这段代码使用伪元素 :before
和 :after
来创建按钮的背景和动态效果。当鼠标悬停在按钮上时,这些伪元素的样式通过 CSS 过渡效果发生变化,从而形成粒子动效。
评论已关闭