Cannot read property 'order' of undefined (48)/ Cannot read property 'length' of undefined (7)
//图片预加载插件
(function($){ //闭包传递参数
function Preload(imgs, options) {
var imgs = (typeof imgs === 'string') ? [imgs] : imgs; //传入对象为字符串
var opts = $.extend({}, Preload.DEFAULTS, options); //将默认的参数和传递进来的参数合并-->后者覆盖前者
if (this.opts.order === 'ordered') { //判断使用有序还是无序 <-- 这里为什么读不到order呢?
this._oredered();
} else {
this._unordered();
}
// this._unordered();
}
Preload.DEFAULTS = {
order: 'unordered', //默认使用无序预加载
each: null, // 每一张图片加载完成
all: null //所有图片加载完成
};
Preload.prototype._oredered = function () { //有序预加载
var imgs = this.imgs,
opts = this.opts,
count= 0,
len = imgs.length; //<--这里 imgs怎么传递的?我这里是undefined
sortPreLoad();
function sortPreLoad () {
var imgObj = new Image();
$(imgObj).on('load error', function () {
opts.each && opts.each(count); //存在each方法,传入count
if(count >= len - 1) { //如果图片加载完成
opts.all && opts.all();
} else {
sortPreLoad();
}
count++;
});
imgObj.src = imgs[count];
};
}
Preload.prototype._unordered = function () { //无序加载
var imgs = this.imgs,
opts = this.opts,
count = 0;
alert(this.imgs)
var 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 Preload(imgs, opts);
}
});
})(jQuery)preload.js:48 Uncaught TypeError: Cannot read property 'length' of undefined
at Preload._unoredered (preload.js:48)
at new Preload (preload.js:12)
at Function.preload (preload.js:72)
at index.html:100
-----
preload.js:7 Uncaught TypeError: Cannot read property 'order' of undefined
at new Preload (preload.js:7)
at Function.preload (preload.js:70)
at index1.html:74
