首页 > 代码库 > jQuery封装图片预加载
jQuery封装图片预加载
(function($) {
function preLoad(imgs, options) {
this.imgs = (typeof imgs === ‘string‘) ? [imgs] : imgs;
this.opts = $.extend({}, preLoad.DEFAULTS, options);
this._unoredered();
}
preLoad.DEFAULTS = {
each: null, //每一张图片加载后执行
all: null //全部图片加载后执行
};
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.src = http://www.mamicode.com/src;
$(imgObj).on(‘load‘, function() {
opts.each && opts.each(count);
if(count >= len - 1) {
opts.all && opts.all();
}
count++;
});
});
};
$.extend({
preload: function(imgs,opts) {
new preLoad(imgs, opts);
}
})
})(jQuery)
jQuery封装图片预加载