码迷,mamicode.com
首页 > Web开发 > 详细

jQuery封装图片预加载

时间:2017-06-26 00:01:11      阅读:248      评论:0      收藏:0      [点我收藏+]

标签:eof   reload   var   str   option   ima   ret   xtend   function   

(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 = 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封装图片预加载

标签:eof   reload   var   str   option   ima   ret   xtend   function   

原文地址:http://www.cnblogs.com/shuzepeng/p/7078417.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!