没事自己搞了一个预加载图片,这个东西貌似比较简单,
好吧,其实就是嫌别人的大,麻烦
code:
//=======================
//疯狂紫萧的懒加载
//为了预加载网页中的图片而设计
//=======================
//预加载
//@pagram list:加载列表,progress:每次加载调用方法,complete:完成之后的回调
//<img class="lazy" data-src="images/4.gif" />
//<div class="lazy" data-background_image="images/5.jpg"></div>
//lazy($(".lazy"), function (num) {load.load(num);}, init);
function lazy(list, progress, complete) {
var list = list || [];
var progress = progress || function () { };
var complete = complete || function () { };
var res = [];
var count = list.length;
var index = 0;
if (list.length == 0)
{
complete && complete(res);
}
for (var i = 0; i < count; i++) {
res[i] = new Image();
res[i].index = i;
if (list[i].dataset.src) {
res[i].onload = function () {
index++;
list[this.index].removeAttribute("data-src");
list[this.index].src = this.src;
progress(index / count * 100);
index >= count && complete();
}
res[i].onerror = function () {
index++;
index >= count && complete();
}
res[i].src = list[i].dataset.src;
}
if (list[i].dataset.background_image) {
res[i].onload = function () {
index++;
list[this.index].removeAttribute("data-background_image");
list[this.index].style.backgroundImage = "url(" + this.src + ")";
progress(index / count * 100);
index >= count && complete();
}
res[i].onerror = function () {
index++;
index >= count && complete(res);
}
res[i].src = list[i].dataset.background_image;
}
}
}