如何在不为每个元素使用第 nth-child 的情况下创建雪花动画场景?

How to create a snowflake animation scene without using nth-child for each element?

有没有一种方法可以在不使用所有 nth-child() 选择器的情况下编写 CSS 代码?我想让代码更具可扩展性,以防将来我想添加更多雪花。

body {
 background-color: red;
}
.snowflake {
  color: #fff;
  font-size: 1em;
  font-family: Arial;
  text-shadow: 0 0 1px #000;
}

@-webkit-keyframes snowflakes-fall {
  0% {
    top: -10%;
  }
  100% {
    top: 100%;
  }
}
@-webkit-keyframes snowflakes-shake {
  0% {
    -webkit-transform: translateX(0px);
    transform: translateX(0px);
  }
  50% {
    -webkit-transform: translateX(80px);
    transform: translateX(80px);
  }
  100% {
    -webkit-transform: translateX(0px);
    transform: translateX(0px);
  }
}

@keyframes snowflakes-fall {
  0% {
    top: -10%;
  }
  100% {
    top: 100%;
  }
}

@keyframes snowflakes-shake {
  0% {
    transform: translateX(0px);
  }
  50% {
    transform: translateX(80px);
  }
  100% {
    transform: translateX(0px);
  }
}

.snowflake {
  position: fixed;
  top: -10%;
  z-index: 9999;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
  cursor: default;
  -webkit-animation-name: snowflakes-fall, snowflakes-shake;
  -webkit-animation-duration: 10s, 3s;
  -webkit-animation-timing-function: linear, ease-in-out;
  -webkit-animation-iteration-count: infinite, infinite;
  -webkit-animation-play-state: running, running;
  animation-name: snowflakes-fall, snowflakes-shake;
  animation-duration: 10s, 3s;
  animation-timing-function: linear, ease-in-out;
  animation-iteration-count: infinite, infinite;
  animation-play-state: running, running;
}

.snowflake:nth-of-type(0) {
  left: 1%;
  -webkit-animation-delay: 0s, 0s;
  animation-delay: 0s, 0s;
}

.snowflake:nth-of-type(1) {
  left: 10%;
  -webkit-animation-delay: 1s, 1s;
  animation-delay: 1s, 1s;
}

.snowflake:nth-of-type(2) {
  left: 20%;
  -webkit-animation-delay: 6s, 0.5s;
  animation-delay: 6s, 0.5s;
}

.snowflake:nth-of-type(3) {
  left: 30%;
  -webkit-animation-delay: 4s, 2s;
  animation-delay: 4s, 2s;
}

.snowflake:nth-of-type(4) {
  left: 40%;
  -webkit-animation-delay: 2s, 2s;
  animation-delay: 2s, 2s;
}

.snowflake:nth-of-type(5) {
  left: 50%;
  -webkit-animation-delay: 8s, 3s;
  animation-delay: 8s, 3s;
}

.snowflake:nth-of-type(6) {
  left: 60%;
  -webkit-animation-delay: 6s, 2s;
  animation-delay: 6s, 2s;
}

.snowflake:nth-of-type(7) {
  left: 70%;
  -webkit-animation-delay: 2.5s, 1s;
  animation-delay: 2.5s, 1s;
}

.snowflake:nth-of-type(8) {
  left: 80%;
  -webkit-animation-delay: 1s, 0s;
  animation-delay: 1s, 0s;
}

.snowflake:nth-of-type(9) {
  left: 90%;
  -webkit-animation-delay: 3s, 1.5s;
  animation-delay: 3s, 1.5s;
}
/* Demo Purpose Only*/
.demo {
  font-family: 'Raleway', sans-serif;
  color: #fff;
  display: block;
  margin: 0 auto;
  padding: 15px 0;
  text-align: center;
}
.demo a {
  font-family: 'Raleway', sans-serif;
  color: #000;
}
 <div class="snowflakes" aria-hidden="true">
      <div class="snowflake">❅</div>
      <div class="snowflake">❅</div>
      <div class="snowflake">❆</div>
      <div class="snowflake">❄</div>
      <div class="snowflake">❅</div>
      <div class="snowflake">❆</div>
      <div class="snowflake">❄</div>
      <div class="snowflake">❅</div>
      <div class="snowflake">❆</div>
      <div class="snowflake">❄</div>
    </div>

首先删除所有不需要的前缀和默认值。那么在这种情况下,您可以使用内联样式结合 CSS 变量来添加特定的 CSS。我还稍微更新了代码以使用 flexbox 并删除所有左侧值:

.snowflakes {
  display: flex;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #000;
  z-index: 9999;
  pointer-events:none;
}
.snowflake {
  color: #fff;
  font-size: 1em;
  font-family: Arial;
  text-shadow: 0 0 1px #000;
  flex: 1;
  position: relative;
  top: -10%;
  animation: snowflakes-fall 10s linear, snowflakes-shake 3s ease-in-out;
  animation-delay: var(--d);
  animation-iteration-count: infinite;
}
@keyframes snowflakes-fall {
  100% {
    top: 100%;
  }
}
@keyframes snowflakes-shake {
  50% {
    transform: translateX(80px);
  }
}
<div class="snowflakes" aria-hidden="true">
  <div class="snowflake" style="--d: 0s, 0s;">❅</div>
  <div class="snowflake" style="--d: 1s, 1s;">❅</div>
  <div class="snowflake" style="--d: 6s, 0.5s;">❆</div>
  <div class="snowflake" style="--d: 4s, 2s;">❄</div>
  <div class="snowflake" style="--d: 2s, 2s;">❅</div>
  <div class="snowflake" style="--d: 8s, 3s;">❆</div>
  <div class="snowflake" style="--d: 6s, 2s;">❄</div>
  <div class="snowflake" style="--d: 2.5s, 1s;">❅</div>
  <div class="snowflake" style="--d: 1s, 0s;">❆</div>
  <div class="snowflake" style="--d: 3s, 1.5s;">❄</div>
</div>