Skip to content

如何实现一朵下雪的云

  1. 首先编写HTML代码如下
html
<section class="snow">
    <div class="loader">
        <div class="s">
            <div style="--i: 11"></div>
            <div style="--i: 12"></div>
            <div style="--i: 15"></div>
            <div style="--i: 17"></div>
            <div style="--i: 18"></div>
            <div style="--i: 13"></div>
            <div style="--i: 14"></div>
            <div style="--i: 19"></div>
            <div style="--i: 20"></div>
            <div style="--i: 10"></div>
            <div style="--i: 16"></div>
            <div style="--i: 9"></div>
            <div style="--i: 3"></div>
            <div style="--i: 6"></div>
            <div style="--i: 8"></div>
            <div style="--i: 7"></div>
            <div style="--i: 1"></div>
            <div style="--i: 2"></div>
            <div style="--i: 4"></div>
            <div style="--i: 5"></div>
        </div>
    </div>
</section>
<section class="snow">
    <div class="loader">
        <div class="s">
            <div style="--i: 11"></div>
            <div style="--i: 12"></div>
            <div style="--i: 15"></div>
            <div style="--i: 17"></div>
            <div style="--i: 18"></div>
            <div style="--i: 13"></div>
            <div style="--i: 14"></div>
            <div style="--i: 19"></div>
            <div style="--i: 20"></div>
            <div style="--i: 10"></div>
            <div style="--i: 16"></div>
            <div style="--i: 9"></div>
            <div style="--i: 3"></div>
            <div style="--i: 6"></div>
            <div style="--i: 8"></div>
            <div style="--i: 7"></div>
            <div style="--i: 1"></div>
            <div style="--i: 2"></div>
            <div style="--i: 4"></div>
            <div style="--i: 5"></div>
        </div>
    </div>
</section>
  1. 编写样式文件
scss
.snow {
  height: 400px;
  background-color: #000;
  margin-top: 20px;
  padding: 100px;
}

.rund {
  height: 600px;
  background-color: #000;
  margin-top: 20px;
}

.snow .loader {
  position: relative;
  width: 110px;
  height: 30px;
  background: #fff;
  border-radius: 100px;
  margin: auto;
}

.snow .loader::before {
  content: "";
  position: absolute;
  width: 30px;
  height: 30px;
  background: white;
  border-radius: 50%;
  box-shadow: 40px 0 0 20px white;
  top: -20px;
  left: 10px;
}

.snow .s {
  position: relative;
  display: flex;
  z-index: 1;
}

.snow .loader .s div {
  position: relative;
  width: 3px;
  height: 3px;
  background: #fff;
  margin: 0 2px;
  border-radius: 50%;
  animation: snow 5s linear infinite;
  //   因为设置了display: flex; 导致block布局变成了flex布局, 所以在子元素宽度没有被撑破的情况下,子元素宽度是有效的,但是当子元素内容过多,此时宽度会比实际宽度小,所以如果想要在已经设置了flex布局的基础上,再进行子元素宽度的设置,可以应用下面的样式:(在该子元素上设置)
  // 设置了(display: flex;)后,子元素如果设置了宽度,在父元素宽度没被撑破时有效,但是一旦撑破,子元素宽度就会失效(会变小),解决方法可以在子元素加个(flex-shrink: 0;)这样就不会失效

  flex-shrink: 0;
}

.snow .loader .s div {
  animation-duration: calc(15s / var(--i));
}

@keyframes snow {
  0% {
    transform: translateY(0);
  }
  70% {
    transform: translateY(100px) scale(1);
  }
  100% {
    transform: translateY(100px) scale(0);
  }
}
.snow {
  height: 400px;
  background-color: #000;
  margin-top: 20px;
  padding: 100px;
}

.rund {
  height: 600px;
  background-color: #000;
  margin-top: 20px;
}

.snow .loader {
  position: relative;
  width: 110px;
  height: 30px;
  background: #fff;
  border-radius: 100px;
  margin: auto;
}

.snow .loader::before {
  content: "";
  position: absolute;
  width: 30px;
  height: 30px;
  background: white;
  border-radius: 50%;
  box-shadow: 40px 0 0 20px white;
  top: -20px;
  left: 10px;
}

.snow .s {
  position: relative;
  display: flex;
  z-index: 1;
}

.snow .loader .s div {
  position: relative;
  width: 3px;
  height: 3px;
  background: #fff;
  margin: 0 2px;
  border-radius: 50%;
  animation: snow 5s linear infinite;
  //   因为设置了display: flex; 导致block布局变成了flex布局, 所以在子元素宽度没有被撑破的情况下,子元素宽度是有效的,但是当子元素内容过多,此时宽度会比实际宽度小,所以如果想要在已经设置了flex布局的基础上,再进行子元素宽度的设置,可以应用下面的样式:(在该子元素上设置)
  // 设置了(display: flex;)后,子元素如果设置了宽度,在父元素宽度没被撑破时有效,但是一旦撑破,子元素宽度就会失效(会变小),解决方法可以在子元素加个(flex-shrink: 0;)这样就不会失效

  flex-shrink: 0;
}

.snow .loader .s div {
  animation-duration: calc(15s / var(--i));
}

@keyframes snow {
  0% {
    transform: translateY(0);
  }
  70% {
    transform: translateY(100px) scale(1);
  }
  100% {
    transform: translateY(100px) scale(0);
  }
}

最终的效果如下