|
| 1 | +const endEvents = []; |
| 2 | + |
| 3 | +const EVENTS = { |
| 4 | + transitionend: { |
| 5 | + transition: `transitionend`, |
| 6 | + WebkitTransition: `webkitTransitionEnd`, |
| 7 | + MozTransition: `mozTransitionEnd`, |
| 8 | + msTransition: `MSTransitionEnd`, |
| 9 | + OTransition: `oTransitionEnd`, |
| 10 | + }, |
| 11 | + |
| 12 | + animationend: { |
| 13 | + animation: `animationend`, |
| 14 | + WebkitAnimation: `webkitAnimationEnd`, |
| 15 | + MozAnimation: `mozAnimationEnd`, |
| 16 | + msAnimation: `MSAnimationEnd`, |
| 17 | + OAnimation: `oAnimationEnd`, |
| 18 | + }, |
| 19 | +}; |
| 20 | + |
| 21 | +if (typeof window !== `undefined`) { |
| 22 | + const style = document.createElement(`div`).style; |
| 23 | + for (let eventType in EVENTS) { |
| 24 | + if (!EVENTS.hasOwnProperty(eventType)) { |
| 25 | + continue |
| 26 | + } |
| 27 | + const prefixes = EVENTS[eventType]; |
| 28 | + for (let styleProp in prefixes) { |
| 29 | + if (prefixes.hasOwnProperty(styleProp) && styleProp in style) { |
| 30 | + endEvents.push(prefixes[styleProp]); |
| 31 | + break; |
| 32 | + } |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | +} |
| 37 | + |
| 38 | +const TransitionEvents = { |
| 39 | + addEndEventListener: function(node, eventListener) { |
| 40 | + if (endEvents.length === 0) { |
| 41 | + setTimeout(eventListener, 0); |
| 42 | + return; |
| 43 | + } |
| 44 | + endEvents.forEach(function(event) { |
| 45 | + node.addEventListener(event, eventListener, false); |
| 46 | + }); |
| 47 | + }, |
| 48 | + |
| 49 | + removeEndEventListener: function(node, eventListener) { |
| 50 | + if (endEvents.length === 0) { |
| 51 | + return; |
| 52 | + } |
| 53 | + endEvents.forEach(function(event) { |
| 54 | + node.removeEventListener(event, eventListener, false); |
| 55 | + }); |
| 56 | + }, |
| 57 | +}; |
| 58 | + |
| 59 | + |
| 60 | +export default TransitionEvents; |
0 commit comments