如何使用 CSS 创建流星动画效果?
流星效果是用于深色主题网站的最酷的背景效果之一。流星动画是加载屏幕的非凡插图,它会在相当长的时间内吸引您的眼球,以便将其余内容加载到网站上。此效果可用于页面加载器、UI。
方法:该方法是使用诸如 webkit-transform 和 transform 之类的 CSS 属性制作小回旋处并将它们的运动对齐 45 度,然后使用 @keyframes 和 @-webkit- 为星的尾部和星的头部(发光部分)添加动画keyframes 属性,现在为拍摄效果添加延迟。这三个属性的基本信息对于本文的进一步讨论至关重要。
HTML 代码:在本节中,我们将创建网页的基本设计。
HTML
Shooting Star Animation
CSS
body {
height: 100vh;
overflow: hidden;
opacity: 0.5;
background-color: black;
display: -webkit-box;
display: flex;
}
/* Here using -webkit, we make angle
of 45 degree of falling star */
.sky {
position: relative;
width: 100%;
height: 100%;
-webkit-transform: rotateZ(45deg);
transform: rotateZ(45deg);
}
/* Here we are making roundabout balls */
.star {
position: absolute;
left: 50%;
top: 50%;
height: 2px;
border-radius: 885px;
background: linear-gradient(-45deg,
#eef0f5, rgba(0, 0, 255, 0));
-webkit-filter: drop-shadow(0 0 6px #eef1f8);
filter: drop-shadow(0 0 6px #d7dff0);
-webkit-animation: tail 3000ms ease-in-out infinite,
shooting 3000ms ease-in-out infinite;
animation: tail 3000ms ease-in-out infinite,
shooting 3000ms ease-in-out infinite;
}
/* Here we add before and after effect to star */
.star::before,
.star::after {
content: "";
position: absolute;
top: calc(50% - 1px);
right: 0;
height: 2px;
background: linear-gradient(
-45deg,
rgba(0, 0, 255, 0),
#eaeef8,
rgba(0, 0, 255, 0)
);
-webkit-transform: translateX(50%) rotateZ(45deg);
transform: translateX(50%) rotateZ(45deg);
border-radius: 100%;
-webkit-animation: shining 3000ms ease-in-out infinite;
animation: shining 3000ms ease-in-out infinite;
}
.star::after {
-webkit-transform: translateX(50%) rotateZ(-45deg);
transform: translateX(50%) rotateZ(-45deg);
}
/* Here we are adding location of each
nth-child, animations, delays, and
before and after effects to each
and every balls(stars) */
.star:nth-child(1) {
top: calc(50% - -119px);
left: calc(50% - 43px);
-webkit-animation-delay: 4000ms;
animation-delay: 4000ms;
}
.star:nth-child(1)::before,
.star:nth-child(1)::after {
-webkit-animation-delay: 4000ms;
animation-delay: 4000ms;
}
.star:nth-child(2) {
top: calc(50% - -43px);
left: calc(50% - 37px);
-webkit-animation-delay: 5000ms;
animation-delay: 5000ms;
}
.star:nth-child(2)::before,
.star:nth-child(2)::after {
-webkit-animation-delay: 5000ms;
animation-delay: 5000ms;
}
.star:nth-child(3) {
top: calc(50% - -40px);
left: calc(50% - 222px);
-webkit-animation-delay: 7000ms;
animation-delay: 7000ms;
}
.star:nth-child(3)::before,
.star:nth-child(3)::after {
-webkit-animation-delay: 7000ms;
animation-delay: 7000ms;
}
.star:nth-child(4) {
top: calc(50% - -29px);
left: calc(50% - 113px);
-webkit-animation-delay: 7000ms;
animation-delay: 7000ms;
}
.star:nth-child(4)::before,
.star:nth-child(4)::after {
-webkit-animation-delay: 7000ms;
animation-delay: 7000ms;
}
.star:nth-child(5) {
top: calc(50% - 146px);
left: calc(50% - 112px);
-webkit-animation-delay: 3000ms;
animation-delay: 3000ms;
}
.star:nth-child(5)::before,
.star:nth-child(5)::after {
-webkit-animation-delay: 3000ms;
animation-delay: 3000ms;
}
.star:nth-child(6) {
top: calc(50% - -108px);
left: calc(50% - 160px);
-webkit-animation-delay: 3000ms;
animation-delay: 3000ms;
}
.star:nth-child(6)::before,
.star:nth-child(6)::after {
-webkit-animation-delay: 3000ms;
animation-delay: 3000ms;
}
.star:nth-child(7) {
top: calc(50% - 52px);
left: calc(50% - 72px);
-webkit-animation-delay: 7000ms;
animation-delay: 7000ms;
}
.star:nth-child(7)::before,
.star:nth-child(7)::after {
-webkit-animation-delay: 7000ms;
animation-delay: 7000ms;
}
/* This code will help to generate
effect in tail of star */
@keyframes tail {
0% {
width: 0;
}
30% {
width: 100px;
}
100% {
width: 0;
}
}
/* This code will generate shining
effect in head of star */
@keyframes shining {
0% {
width: 0;
}
50% {
width: 30px;
}
100% {
width: 0;
}
}
/* Here we are adding webkit-animation
to head of star for shining */
@-webkit-keyframes shining {
0% {
width: 0;
}
50% {
width: 30px;
}
100% {
width: 0;
}
}
/* Here we are adding webkit-animation
to tail of star */
@-webkit-keyframes tail {
0% {
width: 0;
}
30% {
width: 100px;
}
100% {
width: 0;
}
}
/* Here we are adding webkit-animation
for shooting effect */
@-webkit-keyframes shooting {
0% {
-webkit-transform: translateX(0);
transform: translateX(0);
}
100% {
-webkit-transform: translateX(270px);
transform: translateX(270px);
}
}
/* Here we make shooting effect */
@keyframes shooting {
0% {
-webkit-transform: translateX(0);
transform: translateX(0);
}
100% {
-webkit-transform: translateX(270px);
transform: translateX(270px);
}
}
CSS 代码:对于 CSS,请按照以下步骤操作:
- 根据您的要求对齐分区组件。
- 使用border-radius 属性给它们一个圆形。
- 使用关键帧通过增加比例来为球设置动画。没有固定的方法可以做到这一点,您可以根据需要更改不同帧上的比例。
- 使用第 n 个子属性在每个元素的动画之间应用一些延迟。
CSS
body {
height: 100vh;
overflow: hidden;
opacity: 0.5;
background-color: black;
display: -webkit-box;
display: flex;
}
/* Here using -webkit, we make angle
of 45 degree of falling star */
.sky {
position: relative;
width: 100%;
height: 100%;
-webkit-transform: rotateZ(45deg);
transform: rotateZ(45deg);
}
/* Here we are making roundabout balls */
.star {
position: absolute;
left: 50%;
top: 50%;
height: 2px;
border-radius: 885px;
background: linear-gradient(-45deg,
#eef0f5, rgba(0, 0, 255, 0));
-webkit-filter: drop-shadow(0 0 6px #eef1f8);
filter: drop-shadow(0 0 6px #d7dff0);
-webkit-animation: tail 3000ms ease-in-out infinite,
shooting 3000ms ease-in-out infinite;
animation: tail 3000ms ease-in-out infinite,
shooting 3000ms ease-in-out infinite;
}
/* Here we add before and after effect to star */
.star::before,
.star::after {
content: "";
position: absolute;
top: calc(50% - 1px);
right: 0;
height: 2px;
background: linear-gradient(
-45deg,
rgba(0, 0, 255, 0),
#eaeef8,
rgba(0, 0, 255, 0)
);
-webkit-transform: translateX(50%) rotateZ(45deg);
transform: translateX(50%) rotateZ(45deg);
border-radius: 100%;
-webkit-animation: shining 3000ms ease-in-out infinite;
animation: shining 3000ms ease-in-out infinite;
}
.star::after {
-webkit-transform: translateX(50%) rotateZ(-45deg);
transform: translateX(50%) rotateZ(-45deg);
}
/* Here we are adding location of each
nth-child, animations, delays, and
before and after effects to each
and every balls(stars) */
.star:nth-child(1) {
top: calc(50% - -119px);
left: calc(50% - 43px);
-webkit-animation-delay: 4000ms;
animation-delay: 4000ms;
}
.star:nth-child(1)::before,
.star:nth-child(1)::after {
-webkit-animation-delay: 4000ms;
animation-delay: 4000ms;
}
.star:nth-child(2) {
top: calc(50% - -43px);
left: calc(50% - 37px);
-webkit-animation-delay: 5000ms;
animation-delay: 5000ms;
}
.star:nth-child(2)::before,
.star:nth-child(2)::after {
-webkit-animation-delay: 5000ms;
animation-delay: 5000ms;
}
.star:nth-child(3) {
top: calc(50% - -40px);
left: calc(50% - 222px);
-webkit-animation-delay: 7000ms;
animation-delay: 7000ms;
}
.star:nth-child(3)::before,
.star:nth-child(3)::after {
-webkit-animation-delay: 7000ms;
animation-delay: 7000ms;
}
.star:nth-child(4) {
top: calc(50% - -29px);
left: calc(50% - 113px);
-webkit-animation-delay: 7000ms;
animation-delay: 7000ms;
}
.star:nth-child(4)::before,
.star:nth-child(4)::after {
-webkit-animation-delay: 7000ms;
animation-delay: 7000ms;
}
.star:nth-child(5) {
top: calc(50% - 146px);
left: calc(50% - 112px);
-webkit-animation-delay: 3000ms;
animation-delay: 3000ms;
}
.star:nth-child(5)::before,
.star:nth-child(5)::after {
-webkit-animation-delay: 3000ms;
animation-delay: 3000ms;
}
.star:nth-child(6) {
top: calc(50% - -108px);
left: calc(50% - 160px);
-webkit-animation-delay: 3000ms;
animation-delay: 3000ms;
}
.star:nth-child(6)::before,
.star:nth-child(6)::after {
-webkit-animation-delay: 3000ms;
animation-delay: 3000ms;
}
.star:nth-child(7) {
top: calc(50% - 52px);
left: calc(50% - 72px);
-webkit-animation-delay: 7000ms;
animation-delay: 7000ms;
}
.star:nth-child(7)::before,
.star:nth-child(7)::after {
-webkit-animation-delay: 7000ms;
animation-delay: 7000ms;
}
/* This code will help to generate
effect in tail of star */
@keyframes tail {
0% {
width: 0;
}
30% {
width: 100px;
}
100% {
width: 0;
}
}
/* This code will generate shining
effect in head of star */
@keyframes shining {
0% {
width: 0;
}
50% {
width: 30px;
}
100% {
width: 0;
}
}
/* Here we are adding webkit-animation
to head of star for shining */
@-webkit-keyframes shining {
0% {
width: 0;
}
50% {
width: 30px;
}
100% {
width: 0;
}
}
/* Here we are adding webkit-animation
to tail of star */
@-webkit-keyframes tail {
0% {
width: 0;
}
30% {
width: 100px;
}
100% {
width: 0;
}
}
/* Here we are adding webkit-animation
for shooting effect */
@-webkit-keyframes shooting {
0% {
-webkit-transform: translateX(0);
transform: translateX(0);
}
100% {
-webkit-transform: translateX(270px);
transform: translateX(270px);
}
}
/* Here we make shooting effect */
@keyframes shooting {
0% {
-webkit-transform: translateX(0);
transform: translateX(0);
}
100% {
-webkit-transform: translateX(270px);
transform: translateX(270px);
}
}
结合以上两段代码即HTML和CSS代码后得到想要的输出。
输出: