代码拉取完成,页面将自动刷新
核心代码
/**
* 图片预加载
*/
(function ($) {
function Preload(imgs,options) {
this.imgs = (typeof imgs === "string")?[imgs]:imgs;
this.opts = $.extend({},Preload.DEFAULTS,options);
// this._unoredered();
if(this.opts.order === 'ordered'){
this._ordered();
}else {
this._unoredered();
}
}
Preload.DEFAULTS = {
/**
* 默认情况下无序的预加载
*/
order:"unoredered",
/**
* 每张图片加载完毕后执行
*/
each:null,
/**
* 所有图片加载完毕后执行
*/
all:null,
};
Preload.prototype._ordered = function(){
/**
* 有序加载
*/
var opts = this.opts,
imgs = this.imgs,
len = imgs.length,
count = 0;
load();
function load() {
var imgObj = new Image();
$(imgObj).on("load error",function () {
opts.each && opts.each(count);
if(count >= len){
/**
* 所有图片已经加载完毕
*/
opts.all && opts.all();
}else {
load();
}
count++;
});
imgObj.src = imgs[count];
}
};
Preload.prototype._unoredered = function () {
/**
* 无序加载
*/
var imgs = this.imgs,
opts = this.opts,
count = 0,
len = imgs.length;
$.each(imgs,function (i,src) {
if(typeof src != "string") return;
var imgObj = new Image();
$(imgObj).on("load error",function () {
opts.each && opts.each(count);
if(count >= len -1){
opts.all && opts.all();
}
count ++;
});
imgObj.src = src;
});
};
$.extend({
preload:function (imgs,opts) {
new Preload(imgs,opts)
}
})
})(jQuery);
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。