javascript – transitionend事件触发两次

我有以下代码,我的问题是,transitionend事件被触发两次。我不知道是什么原因造成的。我怀疑供应商的前缀造成的,但他们没有。即使我只是离开转型和过渡,它仍然会发射两次。

CSS

transition: 1s ease-out;

JS

document.addEventListener('click', function (e) {
    var submarine = document.querySelector('.submarine');
    var submarineX = e.clientX - submarine.offsetWidth / 2;
    var submarineY = e.clientY - submarine.offsetHeight / 2;

    submarine.style.left = submarineX + "px";
    submarine.style.top = submarineY + "px";
});

document.addEventListener('transitionend', function (event) {
    console.log(event.type + " " + new Date().getTime());
});

Fiddle

document.addEventListener('transitionend', function (event) {
    console.log(event.type + " " + new Date().getTime());
});

document.addEventListener('click', function (e) {
    var submarine = document.querySelector('.submarine');
    var submarineX = e.clientX - submarine.offsetWidth / 2;
    var submarineY = e.clientY - submarine.offsetHeight / 2;

    submarine.style.left = submarineX + "px";
    submarine.style.top = submarineY + "px";
});
.submarine {
    position: absolute;
    top: 0;
    left: 0;
    width: 20px;
    height: 20px;
    background-color: red;
    border-radius: 50%;
    transition: 1s ease-out;
}
<div class="submarine"></div>
每个属性转换的转换火,在您的情况下,上和左。

您可以访问与event.propertyName相关联的事件属性。

没有“transitionsend”事件,所以你可能需要一些hackiness,例如过滤掉只有一个转换属性的转换回调处理。例如。:

function (event) {
    if (event.propertyName == 'top') {
        //put your code here
    }
});

PS。没有浏览器触发MSTransitionEnd事件。在MS文档的某个时候,IE10测试版之前的某个时候被标准的转换事件所取代。

http://stackoverflow.com/questions/18689031/transitionend-event-fires-twice

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:javascript – transitionend事件触发两次