(function () { function (imgs, options) { this.imgs = (typeof imgs === 'string') ? [imgs] : imgs; this.opts = $.extend({}, ProLoad.DEDAULTS, options); if (this.opts.order === 'ordered') { this._ordered(); } else{ this._unordered(); } } ProLoad.DEDAULTS = { order: 'unordered', each: null, all: null }; ProLoad.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 () { if (count >= len) { }else{ load() } count++; }); imgObj.src = imgs[count]; } }; ProLoad.prototype._unordered = 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 ProLoad(imgs,opts); } }) })(jQuery);
|