首页 > Web开发 > 详细

jQuery封装图片预加载

时间:2017-06-25 23:43:45      阅读:307      评论:0      收藏:0      [点我收藏+]

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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!