課程
/前端開發(fā)
/HTML/CSS
/圖片預(yù)加載
代碼按老師說的敲,求大神指點。。。。
2018-02-08
源自:圖片預(yù)加載
正在回答
//圖片預(yù)加載_無序加載
(function($) {
function PreLoad(imgs, options) {
this.imgs = (typeof imgs === 'string') ? [imgs] : imgs;
this.opts = $.extend({}, PreLoad.DEFAULTS, options);
if(this.opts.order === 'ordered') {
this._ordered();
} else {
this._unordered();
}
PreLoad.DEFAULTS = {
order: 'unordered', //默認使用無序預(yù)加載
each: null, //每張圖片加載完畢后執(zhí)行
all: null, //所有圖片加載完畢后執(zhí)行
};
PreLoad.prototype._ordered = function() { //有序加載
var opts = this.opts,
imgs = this.imgs,
len = imgs.lenght,
count = 0;
load();
//有序預(yù)加載
function load() {
var imgObj = new Image();
$(imgObj).on('load error', function() {
opts.each && opts.each(cuont);?
if(count >= len) {
//所以圖片已經(jīng)加載完畢
opts.all && opts.all();
count++
});
imgObj.src = imgs[count];
},
PreLoad.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;
opts.each && opts.each(count);
if(count >= len - 1) {
count++;
})
imgObj.src = src;
$.extend({
preload: function(imgs, opts) {
new PreLoad(imgs, opts);
})(jQuery);
舉報
預(yù)知發(fā)生的行為,提前加載需要的圖片,獲得更好的用戶體驗
1 回答圖片預(yù)加載的原理
2 回答為什么預(yù)加載圖片都變成403
5 回答為什么new Image()后,沒有用src賦值?也可以預(yù)加載?
1 回答執(zhí)行順序是什么?
1 回答請問一下你看這個圖片預(yù)加載效果的軟件是什么
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網(wǎng)安備11010802030151號
購課補貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號
2018-02-08
//圖片預(yù)加載_無序加載
(function($) {
function PreLoad(imgs, options) {
this.imgs = (typeof imgs === 'string') ? [imgs] : imgs;
this.opts = $.extend({}, PreLoad.DEFAULTS, options);
if(this.opts.order === 'ordered') {
this._ordered();
} else {
this._unordered();
}
}
PreLoad.DEFAULTS = {
order: 'unordered', //默認使用無序預(yù)加載
each: null, //每張圖片加載完畢后執(zhí)行
all: null, //所有圖片加載完畢后執(zhí)行
};
PreLoad.prototype._ordered = function() { //有序加載
var opts = this.opts,
imgs = this.imgs,
len = imgs.lenght,
count = 0;
load();
//有序預(yù)加載
function load() {
var imgObj = new Image();
$(imgObj).on('load error', function() {
opts.each && opts.each(cuont);?
if(count >= len) {
//所以圖片已經(jīng)加載完畢
opts.all && opts.all();
} else {
load();
}
count++
});
imgObj.src = imgs[count];
}
},
PreLoad.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 PreLoad(imgs, opts);
}
})
})(jQuery);