小编给大家分享一下CSS3怎么实现流星雨效果,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
说明:正文只讲述单个流星雨的实现方式,多个的效果只需要对单个的动画起始点、宽度稍加修改即可,具体示例见文末 github 地址。
难度系数
☆☆☆☆☆
效果图

思路
流星雨的实现分为三部分:
(1)用 border 属性实现直角三角形。三角形的实现可以参考 CSS绘制三角形
(2)给直角三角形添加圆形效果,弱化直角形状的棱角
(3)添加动画效果,让直角三角形动起来
HTML
<span class="shooting-star animation"></span>
解析:
CSS
.shooting-star {
margin: 30px;
display: block;
width: 0;
border-radius: 2px;
border-top-width: 1px;
border-top-style: solid;
border-top-color: transparent;
border-left-width: 230px;
border-left-style: solid;
border-left-color: white;
border-right-width: 230px;
border-right-style: solid;
border-right-color: transparent;
border-bottom-width: 1px;
border-bottom-style: solid;
border-bottom-color: white;
}
.animation {
animation: fly 3s infinite;
}
@keyframes fly {
from {
margin-left: 900px;
border-left-width: 130px;
border-right-width: 130px;
}
to {
margin-left: -900px;
border-left-width: 360px;
border-right-width: 360px;
}
}
解析:
直角三角形
直角三角形的实现,首先确定直角的方位,本例直角方位为左下角,因此设置左边框和下边框为有颜色的,右边框和上边框为透明色
流星类似一条线的形状,所以直角三角形高度很小,宽度很大。因此此处设置左右边框宽度值很大,上下边框宽度值很小
类似 span 这样 display 默认属性值不为 block 的元素,需要设置 display 属性为 block
圆形效果
动画效果
知识点
CSS 实现三角形
圆角 border
animation 添加动画效果
@keyframes 自定义动画
Gitbub 源码:
https://github.com/nanzhangren/CSS_skills/blob/master/shooting_star/shooting_star.html
以上是“CSS3怎么实现流星雨效果”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注天达云行业资讯频道!