简体   繁体   English

使用具有不同父级的 SASS mixin 的增量转换延迟

[英]Incremental transition-delay using SASS mixins with different parents

I am wanting to auto increment a transition-delay onto any element with a class name 'animate', my issue is where my 'animate' classes have different parents.我想将过渡延迟自动增加到任何类名为“animate”的元素上,我的问题是我的“animate”类有不同的父类。 Is there a way to use a SASS mixin to auto-increment regardless of parent?有没有办法使用 SASS mixin 自动递增而不管父级?

<div class="top-level">
  <div class="first-parent">
      <h1 class="animate">Require a transition-delay of 0.25</h1>
      <p class="animate">Require a transition-delay of 0.5</p>
  </div>
  <div class="second-parent">
      <div class="random-div">
          <div class="animate">Require a transition-delay of 0.75</div>
      </div>
  </div>
</div>

Thanks in advance提前致谢

I think you might have to make do with a substitute instead.我想你可能不得不用替代品来代替。

Here's a way using a Sass variable, incrementing by 0.25 inside a @for loop.这是一种使用 Sass 变量的方法,在@for循环内以 0.25 递增。 This requires manually setting class names in your HTML, ie .animate-1 , .animate-2 etc.:这需要在 HTML 中手动设置类名,即.animate-1.animate-2等:

$counter: 0.25;

@for $i from 1 through 3 {
  $counter: $counter + 0.25;

  .animate-#{$i} {
    transition-delay: $counter + s;
  }
}

Another way is using JS so you don't have to alter the HTML:另一种方法是使用 JS,因此您不必更改 HTML:

 const animate = document.querySelectorAll('.animate'); let increment = 0.25; animate.forEach((i, index) => { i.style.transitionDelay = increment + 's'; increment += 0.25; console.log(`element ${index + 1} transition-delay: `, i.style.transitionDelay); })
 <div class="top-level"> <div class="first-parent"> <h1 class="animate">Require a transition-delay of 0.25</h1> <p class="animate">Require a transition-delay of 0.5</p> </div> <div class="second-parent"> <div class="random-div"> <div class="animate">Require a transition-delay of 0.75</div> </div> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM