小编典典

Transitionend事件触发两次

css

我有以下代码,但我的问题是该transitionend事件被触发了两次。我不知道是什么原因造成的。我怀疑是由供应商前缀引起的,但并非如此。即使我只离开transitionendtransition它仍然会开火两次。

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());
});






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>

阅读 1108

收藏
2020-05-16

共1个答案

小编典典

transitionend对于您top和,每个已转换的属性都会触发left

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

没有“ transition s end”事件,因此您可能需要一些技巧,例如transitionend仅过滤其中一个过渡属性的回调处理。例如:

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

ps。没有浏览器会触发该MSTransitionEnd事件。它是在MS文档中的某个时刻,但是在IE10beta版本发布之前的某个时间,它被标准transitionend事件所取代。

2020-05-16