思路
- 先画出一个类似篮球场的椭圆
.cloud。
- 使用伪元素
:before和box-shadow创建两个小圆,叠放在椭圆.cloud上,构成云朵。
- 使用使用任意多个
span标签模拟小雪花
- 创建
snowing动画,从上到下掉落,到快要结束的距离再将透明度变换到0。
- 使用
animation-duration差异化掉落的时间。
核心技巧:box-shadow、animation-duration
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| <div class="cloud"> <div class="sonw"> <span style="--i:20"></span> <span style="--i:11"></span> <span style="--i:16"></span> <span style="--i:14"></span> <span style="--i:12"></span> <span style="--i:17"></span> <span style="--i:18"></span> <span style="--i:13"></span> <span style="--i:15"></span> <span style="--i:19"></span> </div> </div>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| body { background: black; } .cloud { width: 188px; height: 40px; background: #fff; border-radius: 30px; margin: 100px auto 0 auto; position: relative; } .cloud::before { position: absolute; content: ""; width: 40px; height: 40px; border-radius: 50%; background: #fff; box-shadow: 56px 0 0 22px #fff; top: -22px; left: 42px; }
.sonw span { display: inline-block; width: 4px; height: 6px; border-radius: 1px; background: #fff; margin-left: 10px; animation: snowing 15s infinite linear; animation-duration: calc(15s / var(--i)); } @keyframes snowing { 0% { transform: translateY(0) scale(1); opacity: 1; } 70% { transform: translateY(150px) scale(.5); opacity: .5; } 100% { transform: translateY(150px) scale(0); opacity: 0; } }
|