Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
193 views
in Technique[技术] by (71.8m points)

css - scss - randomize in loop

I am trying to translate this

.snowflake:nth-of-type(0) {
  left: 1%;
  animation-delay: 0s, 0s;
}
.snowflake:nth-of-type(1) {
  left: 40%;
  animation-delay: 1s, 1s;
}
.snowflake:nth-of-type(2) {
  left:80%;
  animation-delay: 6s, 0.5s;
}

to translate and randomize it into scss like

@function random_range($min, $max) {
  $rand: random();
  $random_range: $min + floor($rand * (($max - $min) + 1));
  @return $random_range;
}

$total: 2;

@for $i from 1 through $total {
  .snowflake:nth-of-type(#{$i}) {
    left: percentage($i/$total);
    animation-delay: random_range(0, 8), random_range(0, 2);
  }
}

I somehow can't get the random_range function to go properly. I'd appreciate a hint.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Can't figure out, why the above was not working as expected in Vue app. Made it work with

@function random-decimal($max) {
  @return random($max) / 10;
}

@mixin random-animation-delay(
  $anim1: random-decimal(80),
  $anim2: random-decimal(25)
) {
  -webkit-animation-delay: $anim1 + s, $anim2 + s;
  animation-delay: $anim1 + s, $anim2 + s;
}

$snowflakes: 15;

@for $i from 1 through $snowflakes {
  .snowflake:nth-of-type(#{$i}) {
    left: random(100) + vw;
    margin-left: -40px;
    @include random-animation-delay();
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...