【问题标题】:SCSS particles animation - create at random position, but move all particles in the same directionSCSS 粒子动画 - 在随机位置创建,但沿同一方向移动所有粒子
【发布时间】:2021-12-10 20:58:44
【问题描述】:

我对 SCSS 还很陌生,所以希望有人可以帮助我。 我正在尝试将粒子朝同一个方向(向下)移动,但是因为所有粒子都是随机放置的,所以它们的目的地也是随机的。

我想保持随机放置,但是一旦放置它们,我希望它们都朝着相同的方向(向下)而不是随机方向。我不知道如何在不丢失步骤生成的随机定位的情况下做到这一点。

我知道如何在 CSS 中执行此操作,但这需要手动指定每个路径,如果我可以让它与 SCSS 一起使用,这将是我的最终解决方案,除非有人可以帮助我?

这里是 JSFiddle 来展示它的行为方式,但这是创建随机步骤的部分:

SCSS

@for $i from 1 through $dot-count {
  &:nth-of-type(#{$i}) {
    animation-name: move-path-#{$i};
    &:before {
      animation-duration: random(5000) + 5000ms, random(1000) + 7000ms;
      animation-delay: random(6000) + 500ms, 0s;
    }
  }
  $steps: random(15) + 10;
  @keyframes move-path-#{$i} {
    @for $step from 0 through $steps {
      #{$step / $steps * 100%} {
        transform: translateX(random(100) - 50vw)
          translateY(random(100) - 50vh)
          scale(random(70) / 100 + 0.3);
      }
    }
  }
}

【问题讨论】:

  • 你是说你想让每个点从一个随机的地方开始,但之后它会直线下降,还是以随机模式下降但总是向下?
  • 是的,第一件事。被放置在一个随机的位置然后摔倒。
  • 他的意思是“直线下降”(如|)或“随机下降”(如\ / |)的下降方向
  • 我明白了,是的,就像 |

标签: css animation sass particles


【解决方案1】:

您可以定义一个所有粒子共有的随机变量,使用它来定位您的粒子,然后根据它们的步数而不是随机移动它们。

html {
  font-size: 30vmax / 1920 * 100;
  font-family: "Quicksand", sans-serif;
  background-color: #000;
  margin: 0;
  padding: 0;
  @media (max-width: 768px) {
    font-size: 50px;
  }
}

// Stars
$dot-color: #fff;
$dot-count: 35;

#milky-way {
  width: 100%;
}

.sky {
  position: fixed;
  width: 100%;
  height: 100%;
  z-index: 10;
  pointer-events: none;
}
.dot {
  position: absolute;
  width: 0.08rem;
  height: 0.08rem;
  top: 50%;
  left: 50%;
  animation: 160s ease both infinite alternate;
  &:before {
    content: "";
    display: block;
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background-color: $dot-color;
    transform-origin: -1rem;
    animation: lighting ease both infinite, auto-rotating ease both infinite;
  }
  @for $i from 1 through $dot-count {
    // Random variable common to all particles
    $random: random(100);
    &:nth-of-type(#{$i}) {
      animation-name: move-path-#{$i};
      &:before {
        animation-duration: random(5000) + 5000ms, random(1000) + 7000ms;
        animation-delay: random(6000) + 500ms, 0s; // less than max duration
      }
    }
    $steps: random(15) + 10;
    @keyframes move-path-#{$i} {
      
      @for $step from 0 through $steps {
        
        #{$step / $steps * 100%} {
          // first translation is depending on a common random, second is depending on the step number
          transform: translateX($random - 50vw) translateX($step * $step * 150px)
            translateY($random - 50vh) translateY($step * $step * 150px)
            scale(random(70) / 100 + 0.3);
        }
      }
    }
  }
}

@keyframes lighting {
  0%,
  30%,
  100% {
    opacity: 0;
  }
  5% {
    opacity: 1;
    box-shadow: 0 0 0.3rem 0.05rem $dot-color;
  }
}
@keyframes auto-rotating {
  to {
    transform: rotate(0deg);
  }
}

【讨论】:

  • 非常感谢您发布您的答案!粒子现在向下和向右移动。我能够通过将 translateX 更改为 0px 并将 translateY 更改为 500px 来使它们全部下降,它们现在正在按应有的方式移动。但是现在粒子似乎有点斜向生成并且动画在几秒钟后停止(即使它具有无限属性),您是否也知道如何解决这个问题?不管怎样,谢谢你,你真的帮了我很多!
  • 您好,很抱歉我没有太多时间进一步调查,希望您能理解。
  • 是的,当然,感谢您的帮助。我决定用普通的 CSS 来做这个,所以我很好。
猜你喜欢
  • 1970-01-01
  • 2014-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-11
  • 1970-01-01
相关资源
最近更新 更多