, . :
document.querySelector('a').addEventListener('click', function(e){
this.classList.toggle('animate');
let style = window.getComputedStyle(this, null);
Promise.all(style.transition.split(',').map((prop) => {
prop = prop.trim().split(/\s+/);
return Promise.race([
new Promise((resolve) => {
let h = (e) => {
if (e.propertyName == prop[0])
resolve('transitionEnd ' + prop[0]);
};
this.addEventListener('transitionend', h, {once:false});
}),
new Promise((resolve) =>
setTimeout(
() => resolve('TIMEOUT ' + prop[0]),
prop[1].replace(/s/,'')*1000 + 100
)
)
])
}))
.then((res) => {
console.log(res + 'DONE!!!!');
});
});
:
- , , // . , : " 4s 0 , 2 0 ,.."
- Promise.race, transitionEnd ( prop , prop (background background-color), ) tp
- , - /eventListeners
:
- this is an example, I admit that he uses a lot of magic, and missed a lot of checks
- The callback will be triggered even if the animation has been canceled (the timeout is triggered anyway)
- requires ES6 :)
you can play with him on jsfiddle
source
share