阿富

web前端

导航

【事件】图片complete事件

/*
 *
 * 图片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时,将不再触发该函数)
});

 

posted on 2016-09-27 11:17  阿富  阅读(236)  评论(0)    收藏  举报