jQuery封装图片预加载
2021-07-13 14:06
标签:eof reload var str option ima ret xtend function (function($) { } }; imgObj.src = src; } }); })(jQuery) jQuery封装图片预加载 标签:eof reload var str option ima ret xtend function 原文地址:http://www.cnblogs.com/shuzepeng/p/7078417.html
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).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);
}
})
下一篇:一张图教会CSS3倒影