[前端css-4] 实现粒子button按钮
1.效果展示:
![在这里插入图片描述](https://img-blog.csdnimg.cn/547246be1c2e4cd5b4376cce868b2930.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_Q1NETiBAVmlvbGVudF9jbG93bg==,size_26,color_FFFFFF,t_70,g_se,x_16#pic_center)
效果预览
2.知识点:
1.position(相对定位/绝对定位)
2.伪类(:active/:hover)
3.animation(动画)
3.源代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="apple-mobile-web-app-title" content="CodePen">
<title>CSS粒子按钮</title>
<style>
@import url("https://fonts.googleapis.com/css?family=Montserrat:400, 600");
.hero-btn {
position: relative;
width: 200px;
margin: auto;
height: 66px;
}
.hero-btn .btn {
background-color: #ff0083;
border: none;
outline: none;
text-decoration: none;
text-transform: uppercase;
color: #fff;
display: inline-block;
width: 200px;
height: 66px;
text-align: center;
line-height: 66px;
font-size: 22px;
font-family: "Montserrat", sans-serif;
font-weight: 600;
letter-spacing: 2px;
-moz-border-radius: 6px;
-webkit-border-radius: 6px;
border-radius: 6px;
white-space: nowrap;
text-overflow: ellipsis;
-moz-user-select: -moz-none;
-ms-user-select: none;
-webkit-user-select: none;
user-select: none;
-moz-appearance: none;
cursor: pointer;
-webkit-appearance: none;
-moz-box-shadow: 0 15px 40px rgba(255, 0, 131, 0.5);
-webkit-box-shadow: 0 15px 40px rgba(255, 0, 131, 0.5);
box-shadow: 0 15px 40px rgba(255, 0, 131, 0.5);
-moz-transform: perspective(600px) rotateX(0deg) scale(1);
-ms-transform: perspective(600px) rotateX(0deg) scale(1);
-webkit-transform: perspective(600px) rotateX(0deg) scale(1);
transform: perspective(600px) rotateX(0deg) scale(1);
-moz-transition: all 0.2s linear;
-o-transition: all 0.2s linear;
-webkit-transition: all 0.2s linear;
transition: all 0.2s linear;
}
.hero-btn .btn:hover {
background-color: #e60076;
-moz-box-shadow: 0 20px 40px rgba(255, 0, 131, 0.5);
-webkit-box-shadow: 0 20px 40px rgba(255, 0, 131, 0.5);
box-shadow: 0 20px 40px rgba(255, 0, 131, 0.5);
}
.hero-btn .btn:active {
-moz-transform: perspective(600px) rotateX(15deg) scale(0.9);
-ms-transform: perspective(600px) rotateX(15deg) scale(0.9);
-webkit-transform: perspective(600px) rotateX(15deg) scale(0.9);
transform: perspective(600px) rotateX(15deg) scale(0.9);
font-size: 21px;
-moz-box-shadow: 0 20px 40px rgba(255, 0, 131, 0.5);
-webkit-box-shadow: 0 20px 40px rgba(255, 0, 131, 0.5);
box-shadow: 0 20px 40px rgba(255, 0, 131, 0.5);
}
.active .hero-btn .btn:active {
-moz-transform: perspective(600px) rotateX(0deg) scale(1);
-ms-transform: perspective(600px) rotateX(0deg) scale(1);
-webkit-transform: perspective(600px) rotateX(0deg) scale(1);
transform: perspective(600px) rotateX(0deg) scale(1);
font-size: 22px;
-moz-box-shadow: 0 15px 40px rgba(255, 0, 131, 0.5);
-webkit-box-shadow: 0 15px 40px rgba(255, 0, 131, 0.5);
box-shadow: 0 15px 40px rgba(255, 0, 131, 0.5);
}
.hero-btn .particles-circle {
position: absolute;
background-color: #ff0083;
width: 30px;
height: 30px;
top: 10px;
left: 50%;
margin-left: -15px;
z-index: -1;
-moz-border-radius: 50%;
-webkit-border-radius: 50%;
border-radius: 50%;
-moz-transform: scale(0);
-ms-transform: scale(0);
-webkit-transform: scale(0);
transform: scale(0);
visibility: hidden;
}
.hero-btn .particles-circle:nth-of-type(odd) {
border: solid 2px #ff0083;
background-color: transparent;
}
@keyframes particles-1 {
from {
transform: scale(1);
visibility: visible;
}
to {
left: -97px;
top: 40px;
transform: scale(0);
visibility: hidden;
}
}
.active .hero-btn .particles-circle:nth-of-type(1) {
animation: particles-1 1.5s 0.025s;
}
@keyframes particles-2 {
from {
transform: scale(1);
visibility: visible;
}
to {
left: -17px;
top: 165px;
transform: scale(0);
visibility: hidden;
}
}
.active .hero-btn .particles-circle:nth-of-type(2) {
animation: particles-2 1.5s 0.05s;
}
@keyframes particles-3 {
from {
transform: scale(1);
visibility: visible;
}
to {
left: -72px;
top: 110px;
transform: scale(0);
visibility: hidden;
}
}
.active .hero-btn .particles-circle:nth-of-type(3) {
animation: particles-3 1.5s 0.075s;
}
@keyframes particles-4 {
from {
transform: scale(1);
visibility: visible;
}
to {
left: -69px;
top: -75px;
transform: scale(0);
visibility: hidden;
}
}
.active .hero-btn .particles-circle:nth-of-type(4) {
animation: particles-4 1.5s 0.1s;
}
@keyframes particles-5 {
from {
transform: scale(1);
visibility: visible;
}
to {
left: -155px;
top: 185px;
transform: scale(0);
visibility: hidden;
}
}
.active .hero-btn .particles-circle:nth-of-type(5) {
animation: particles-5 1.5s 0.125s;
}
@keyframes particles-6 {
from {
transform: scale(1);
visibility: visible;
}
to {
left: 168px;
top: -143px;
transform: scale(0);
visibility: hidden;
}
}
.active .hero-btn .particles-circle:nth-of-type(6) {
animation: particles-6 1.5s 0.15s;
}
@keyframes particles-7 {
from {
transform: scale(1);
visibility: visible;
}
to {
left: -39px;
top: 139px;
transform: scale(0);
visibility: hidden;
}
}
.active .hero-btn .particles-circle:nth-of-type(7) {
animation: particles-7 1.5s 0.175s;
}
@keyframes particles-8 {
from {
transform: scale(1);
visibility: visible;
}
to {
left: 160px;
top: 77px;
transform: scale(0);
visibility: hidden;
}
}
.active .hero-btn .particles-circle:nth-of-type(8) {
animation: particles-8 1.5s 0.2s;
}
@keyframes particles-9 {
from {
transform: scale(1);
visibility: visible;
}
to {
left: -229px;
top: -168px;
transform: scale(0);
visibility: hidden;
}
}
.active .hero-btn .particles-circle:nth-of-type(9) {
animation: particles-9 1.5s 0.225s;
}
@keyframes particles-10 {
from {
transform: scale(1);
visibility: visible;
}
to {
left: -151px;
top: 95px;
transform: scale(0);
visibility: hidden;
}
}
.active .hero-btn .particles-circle:nth-of-type(10) {
animation: particles-10 1.5s 0.25s;
}
@keyframes particles-11 {
from {
transform: scale(1);
visibility: visible;
}
to {
left: -137px;
top: -62px;
transform: scale(0);
visibility: hidden;
}
}
.active .hero-btn .particles-circle:nth-of-type(11) {
animation: particles-11 1.5s 0.275s;
}
@keyframes particles-12 {
from {
transform: scale(1);
visibility: visible;
}
to {
left: 157px;
top: 143px;
transform: scale(0);
visibility: hidden;
}
}
.active .hero-btn .particles-circle:nth-of-type(12) {
animation: particles-12 1.5s 0.3s;
}
@keyframes particles-13 {
from {
transform: scale(1);
visibility: visible;
}
to {
left: -185px;
top: 117px;
transform: scale(0);
visibility: hidden;
}
}
.active .hero-btn .particles-circle:nth-of-type(13) {
animation: particles-13 1.5s 0.325s;
}
@keyframes particles-14 {
from {
transform: scale(1);
visibility: visible;
}
to {
left: -208px;
top: 158px;
transform: scale(0);
visibility: hidden;
}
}
.active .hero-btn .particles-circle:nth-of-type(14) {
animation: particles-14 1.5s 0.35s;
}
@keyframes particles-15 {
from {
transform: scale(1);
visibility: visible;
}
to {
left: 101px;
top: -249px;
transform: scale(0);
visibility: hidden;
}
}
.active .hero-btn .particles-circle:nth-of-type(15) {
animation: particles-15 1.5s 0.375s;
}
@keyframes particles-16 {
from {
transform: scale(1);
visibility: visible;
}
to {
left: 20px;
top: -27px;
transform: scale(0);
visibility: hidden;
}
}
.active .hero-btn .particles-circle:nth-of-type(16) {
animation: particles-16 1.5s 0.4s;
}
@keyframes particles-17 {
from {
transform: scale(1);
visibility: visible;
}
to {
left: -147px;
top: -216px;
transform: scale(0);
visibility: hidden;
}
}
.active .hero-btn .particles-circle:nth-of-type(17) {
animation: particles-17 1.5s 0.425s;
}
@keyframes particles-18 {
from {
transform: scale(1);
visibility: visible;
}
to {
left: 201px;
top: -26px;
transform: scale(0);
visibility: hidden;
}
}
.active .hero-btn .particles-circle:nth-of-type(18) {
animation: particles-18 1.5s 0.45s;
}
@keyframes particles-19 {
from {
transform: scale(1);
visibility: visible;
}
to {
left: -36px;
top: -224px;
transform: scale(0);
visibility: hidden;
}
}
.active .hero-btn .particles-circle:nth-of-type(19) {
animation: particles-19 1.5s 0.475s;
}
@keyframes particles-20 {
from {
transform: scale(1);
visibility: visible;
}
to {
left: 19px;
top: 64px;
transform: scale(0);
visibility: hidden;
}
}
.active .hero-btn .particles-circle:nth-of-type(20) {
animation: particles-20 1.5s 0.5s;
}
@keyframes particles-21 {
from {
transform: scale(1);
visibility: visible;
}
to {
left: -99px;
top: 85px;
transform: scale(0);
visibility: hidden;
}
}
.active .hero-btn .particles-circle:nth-of-type(21) {
animation: particles-21 1.5s 0.525s;
}
@keyframes particles-22 {
from {
transform: scale(1);
visibility: visible;
}
to {
left: 244px;
top: 97px;
transform: scale(0);
visibility: hidden;
}
}
.active .hero-btn .particles-circle:nth-of-type(22) {
animation: particles-22 1.5s 0.55s;
}
@keyframes particles-23 {
from {
transform: scale(1);
visibility: visible;
}
to {
left: 156px;
top: -132px;
transform: scale(0);
visibility: hidden;
}
}
.active .hero-btn .particles-circle:nth-of-type(23) {
animation: particles-23 1.5s 0.575s;
}
@keyframes particles-24 {
from {
transform: scale(1);
visibility: visible;
}
to {
left: -162px;
top: 51px;
transform: scale(0);
visibility: hidden;
}
}
.active .hero-btn .particles-circle:nth-of-type(24) {
animation: particles-24 1.5s 0.6s;
}
@keyframes particles-25 {
from {
transform: scale(1);
visibility: visible;
}
to {
left: -117px;
top: 143px;
transform: scale(0);
visibility: hidden;
}
}
.active .hero-btn .particles-circle:nth-of-type(25) {
animation: particles-25 1.5s 0.625s;
}
@keyframes particles-26 {
from {
transform: scale(1);
visibility: visible;
}
to {
left: 124px;
top: -192px;
transform: scale(0);
visibility: hidden;
}
}
.active .hero-btn .particles-circle:nth-of-type(26) {
animation: particles-26 1.5s 0.65s;
}
@keyframes particles-27 {
from {
transform: scale(1);
visibility: visible;
}
to {
left: -249px;
top: -97px;
transform: scale(0);
visibility: hidden;
}
}
.active .hero-btn .particles-circle:nth-of-type(27) {
animation: particles-27 1.5s 0.675s;
}
@keyframes particles-28 {
from {
transform: scale(1);
visibility: visible;
}
to {
left: 8px;
top: 135px;
transform: scale(0);
visibility: hidden;
}
}
.active .hero-btn .particles-circle:nth-of-type(28) {
animation: particles-28 1.5s 0.7s;
}
@keyframes particles-29 {
from {
transform: scale(1);
visibility: visible;
}
to {
left: 81px;
top: -112px;
transform: scale(0);
visibility: hidden;
}
}
.active .hero-btn .particles-circle:nth-of-type(29) {
animation: particles-29 1.5s 0.725s;
}
@keyframes particles-30 {
from {
transform: scale(1);
visibility: visible;
}
to {
left: 39px;
top: 19px;
transform: scale(0);
visibility: hidden;
}
}
.active .hero-btn .particles-circle:nth-of-type(30) {
animation: particles-30 1.5s 0.75s;
}
@keyframes particles-31 {
from {
transform: scale(1);
visibility: visible;
}
to {
left: -82px;
top: 46px;
transform: scale(0);
visibility: hidden;
}
}
.active .hero-btn .particles-circle:nth-of-type(31) {
animation: particles-31 1.5s 0.775s;
}
@keyframes particles-32 {
from {
transform: scale(1);
visibility: visible;
}
to {
left: -48px;
top: 136px;
transform: scale(0);
visibility: hidden;
}
}
.active .hero-btn .particles-circle:nth-of-type(32) {
animation: particles-32 1.5s 0.8s;
}
@keyframes particles-33 {
from {
transform: scale(1);
visibility: visible;
}
to {
left: 28px;
top: -26px;
transform: scale(0);
visibility: hidden;
}
}
.active .hero-btn .particles-circle:nth-of-type(33) {
animation: particles-33 1.5s 0.825s;
}
@keyframes particles-34 {
from {
transform: scale(1);
visibility: visible;
}
to {
left: 44px;
top: -10px;
transform: scale(0);
visibility: hidden;
}
}
.active .hero-btn .particles-circle:nth-of-type(34) {
animation: particles-34 1.5s 0.85s;
}
@keyframes particles-35 {
from {
transform: scale(1);
visibility: visible;
}
to {
left: -98px;
top: -76px;
transform: scale(0);
visibility: hidden;
}
}
.active .hero-btn .particles-circle:nth-of-type(35) {
animation: particles-35 1.5s 0.875s;
}
.v-align {
position: absolute;
text-align: center;
left: 0;
width: 100%;
max-height: 90vh;
top: 50%;
-moz-transform: translateY(-50%);
-ms-transform: translateY(-50%);
-webkit-transform: translateY(-50%);
transform: translateY(-50%);
}
.wrap__sparks {
transform: translateZ(-100px);
}
* {
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
box-sizing: border-box;
}
html,
body {
min-height: 100%;
}
body {
background: #f8faff;
height: 100vh;
position: relative;
}
</style>
</head>
<body translate="no">
<div class='v-align'>
<div class='hero-btn'>
<button class='btn'>
小洪同学
</button>
<span class='particles-circle'></span>
<span class='particles-circle'></span>
<span class='particles-circle'></span>
<span class='particles-circle'></span>
<span class='particles-circle'></span>
<span class='particles-circle'></span>
<span class='particles-circle'></span>
<span class='particles-circle'></span>
<span class='particles-circle'></span>
<span class='particles-circle'></span>
<span class='particles-circle'></span>
<span class='particles-circle'></span>
<span class='particles-circle'></span>
<span class='particles-circle'></span>
<span class='particles-circle'></span>
<span class='particles-circle'></span>
<span class='particles-circle'></span>
<span class='particles-circle'></span>
<span class='particles-circle'></span>
<span class='particles-circle'></span>
<span class='particles-circle'></span>
<span class='particles-circle'></span>
<span class='particles-circle'></span>
<span class='particles-circle'></span>
<span class='particles-circle'></span>
<span class='particles-circle'></span>
<span class='particles-circle'></span>
<span class='particles-circle'></span>
<span class='particles-circle'></span>
<span class='particles-circle'></span>
<span class='particles-circle'></span>
<span class='particles-circle'></span>
<span class='particles-circle'></span>
<span class='particles-circle'></span>
<span class='particles-circle'></span>
</div>
</div>
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js'></script>
<script id="rendered-js">
$('.hero-btn').click(function () {
if (!$('.hero-btn').parent().hasClass('active')) {
$(this).parent().stop().addClass('active');
setTimeout(function () {
$('.hero-btn').parent().removeClass('active');
}, 2000);
}
});
</script>
</body>
</html>
4.相关知识点学习资料: