Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
154
rated 0 times [  155] [ 1]  / answers: 1 / hits: 23969  / 11 Years ago, sun, september 8, 2013, 12:00:00

I have the following code and my problem is that the transitionend event is fired twice. I don't know what's causing this. I suspected the vendor prefixes caused it, but they don't. Even if I only leave transitionend and transition it will still fire twice.



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>




More From » css

 Answers
7

transitionend fires for each property transitioned, in your case top and left.



You can access the property associated with the event at event.propertyName.



There's no transitionsend event, so you will probably need some hackiness such as filtering the transitionend callback handling for only one of the transitioned properties. E.g.:



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





ps. No browser fires the MSTransitionEnd event. It was at some point in the MS docs, but sometime before the IE10 beta release it was replaced by the standard transitionend event.


[#75825] Friday, September 6, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jimmiejudahm

Total Points: 319
Total Questions: 98
Total Answers: 117

Location: Venezuela
Member since Sat, Apr 24, 2021
3 Years ago
;