另一种符合纯特征检测(而不是纯实用解决方案)但更有效的变体:
var transitionendNames = [
'transitionend',
'webkitTransitionEnd',
'MozTransitionEnd',
'oTransitionEnd'
];
/**
* Helper function to bind to the correct transitionend event
* @param {function} callback The function to call when the event fires
*/
var transitionend = function(elem, callback) {
var handler = function(e) {
//console.log('transitionend == '+e.type);
// test in case multiple were registered before change
if (transitionendNames) {
// store the actual name
var transitionendName = e.type;
// every other time, bind only to actual event
transitionend = function(elem, callback) {
elem.addEventListener(transitionendName, callback, false);
};
// flag for any others
transitionendNames = null;
}
return callback.call(elem, e);
};
// until an event has been triggered bind them all
for (var i=0, len=transitionendNames.length; i<len; i++) {
elem.addEventListener(transitionendNames[i], handler, false);
}
};