为了账号安全,请及时绑定邮箱和手机立即绑定

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

正在回答

1 回答

http://img1.sycdn.imooc.com//591d847500014aef08250248.jpg

这不是已经告诉你错误在哪了吗?

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
图片预加载
  • 参与学习       40987    人
  • 解答问题       81    个

预知发生的行为,提前加载需要的图片,获得更好的用户体验

进入课程

Cannot read property 'order' of undefined (48)/ Cannot read property 'length' of undefined (7)

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信