/*
*
* 图片complete事件
* 回调的返回值为'off'则事件被解除
*
*/
var imgComplete = function(el, callback) {
if ((el && el.nodeName) !== 'IMG' || typeof callback !== 'function') {
return el;
}
if (el.complete) {
callback.call(el);
} else {
var fEvent = function() {
var result = callback.call(el);
if (result === 'off') {
el.removeEventListener('load', fEvent);
el.removeEventListener('error', fEvent);
}
};
el.addEventListener('load', fEvent);
el.addEventListener('error', fEvent);
}
return el;
};
//使用情况
imgComplete(el, function() {
console.log('图片加载完成');
return 'off'; //事件被解除(动态改变src时,将不再触发该函数)
});