检测子元素动画

Detecting child element animation

本文关键字:动画 元素 检测      更新时间:2023-09-26

我有一个父div里面有从左到右动画的子div。如何检测子div是否为动画。子div正在移动,并且子div样式中的左侧位置不断变化,因为它们从左向右移动。

<div id="products">
<div id="product1 style="color:blue;left:10px;"></div>
<div id="product2 style="color:blue;left:20px;"></div>
<div id="product3 style="color:blue;left:30px;"></div>
</div>

左边的数字正在变化,有没有办法检测到变化。提前感谢

我在手机上,没有测试这个,但它应该可以工作

var a = document.getElementById('products').querySelectorAll('div');
for (var i=0;i<a.length;i++){
    var now = a[i].style.left;
    setTimeout(function(){
         if (a[i].style.left != now)
         {
             // the element is animated
         }
    },2);
}