【问题标题】:How can I get TransitionEnd to work on EACH item in a collection, instead of only firing on the last item? [duplicate]如何让 TransitionEnd 在集合中的每个项目上工作,而不是只在最后一个项目上触发? [复制]
【发布时间】:2022-01-11 02:04:57
【问题描述】:

我需要元素进行转换(添加一个类),然后在完成所述转换后恢复(通过删除类)。这有效,但仅适用于集合中的最后一个元素(无论有多少)。如何让 transitionEnd 在每个元素上触发,而不是仅在最后一个元素上触发?

我已经尝试了各种超时等来代替 .on('webkitTransitionEnd... 到目前为止没有任何效果。

我不能按顺序触发它们,因为它会花费太长时间。有几十个需要同时发射。

有没有办法让这个排队,还是我完全用错误的方式接近这个?

在实际应用程序中,文本会在循环之间发生变化和其他事情,这就是为什么可以使用关键帧让它向下摆动,等待然后再向上。

提前致谢,请告知我是否应该以不同的方式发布/措辞这个问题。

$(document).on("click", "#one", function(e) {
  flipEach()
});


function flipEach(){ 
  // itterate through an array of same-class elements and execute on each
  $(".card").each(function( index ) { 
    // use the index to itterate through the IDs
    position = "#pos_"+(index+1)
    // add the transition class to current item in the each/array
    $( position ).addClass('flipped')
    // change text and remove item on the current item in the each/array after transitionEnd
    $( position ).on('webkitTransitionEnd otransitionend oTransitionEnd msTransitionEnd transitionend',   
      function(e) {
      // remove the class that flipped it and restore position
      $( position ).removeClass("flipped");  
    });  
});  
};
body {
  display:flex;
  align-items: center;
  text-align: center;
}

.card {
  width:80px;
  height:120px;
  margin:10px;
  font-size:50px;
  text-align:center;
  background-color: gray;
  transform-origin: 0% 100%;
}

.flipped{
  transform: rotateX(-180deg); 
  transition-property: transform;
  transition-duration: 1s; 
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button id="one">fipEach</button>

<div id="pos_1" class="card">1</div>
<div id="pos_2" class="card">2</div>
<div id="pos_3" class="card">3</div>
<div id="pos_4" class="card">4</div>

【问题讨论】:

  • position 是一个 全局 变量。当任何转换结束时,您为什么希望它的值对应于当前元素而不是 position 的最后一个值?为什么不使用$(document).on("transitionend", ".card", function(){ $(this).removeClass("flipped"); }); 或类似的东西?
  • @Sebastian Simon,因为我们有时都会犯错,当我们盯着它看太久时,我们都会错过明显的东西......好点!感谢您的反馈:-)

标签: javascript jquery css css-transitions transitionend


【解决方案1】:

您的 position 变量搞砸了。由于您只是试图使用它来引用当前被迭代的元素,因此它完全是多余的 - 只需使用 this 来引用该元素,然后将类和处理程序添加到该元素。

$(document).on("click", "#one", function(e) {
  flipEach()
});


function flipEach() {
  // itterate through an array of same-class elements and execute on each
  $(".card").each(function() {
    // add the transition class to current item in the each/array
    $(this).addClass('flipped')
    // change text and remove item on the current item in the each/array after transitionEnd
    $(this).on('webkitTransitionEnd otransitionend oTransitionEnd msTransitionEnd transitionend',
      function(e) {
        // remove the class that flipped it and restore position
        $(this).removeClass("flipped");
      });
  });
};
body {
  display: flex;
  align-items: center;
  text-align: center;
}

.card {
  width: 80px;
  height: 120px;
  margin: 10px;
  font-size: 50px;
  text-align: center;
  background-color: gray;
  transform-origin: 0% 100%;
}

.flipped {
  transform: rotateX(-180deg);
  transition-property: transform;
  transition-duration: 1s;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button id="one">fipEach</button>

<div id="pos_1" class="card">1</div>
<div id="pos_2" class="card">2</div>
<div id="pos_3" class="card">3</div>
<div id="pos_4" class="card">4</div>

【讨论】:

  • 天啊,当你指出它时,它是如此清晰和明显!我一直专注于将这些元素的 ID 用于代码的其他部分,我什至没有意识到这里根本不需要它们。谢谢一百万,它就像一个魅力!
猜你喜欢
  • 1970-01-01
  • 2016-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-21
  • 2013-07-13
  • 2022-11-10
相关资源
最近更新 更多