图片分步加载(解决图片load函数刷新不加载问题)

/**
* 高清图加载完成替换模糊图
* @param showImg 高清图类名 图片一定要有dataindex属性
* */
function loadImgHighDefinition(showImg){
var imgdefereds=[];
var showImgObj = "." + showImg;
$(showImgObj).each(function(){
var dfd=$.Deferred();
$(this).bind('load',function(){
var _this = $(this);
var newoBGiMG = showImg + _this.attr('dataindex');
console.log(newoBGiMG)
$('.'+newoBGiMG).attr('src',_this.attr('src'));
_this.removeClass(showImg);
dfd.resolve();
}).bind('error',function(){
//图片加载错误,加入错误处理
// dfd.resolve();
})
if(this.complete) setTimeout(function(){
dfd.resolve();
},500);
imgdefereds.push(dfd);
})
$.when.apply(null,imgdefereds).done(function(){
callback();
});
function callback(){
$(showImgObj).each(function(){
var _this = $(this);
var newoBGiMG = showImg + _this.attr('dataindex');
$('.'+newoBGiMG).attr('src',_this.attr('src'));
_this.removeClass(showImg);
});
}
}

原文地址:https://www.cnblogs.com/xiaomixia/p/8677007.html