最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

javascript preload&lazy load

 更新時間:2010年05月13日 01:29:02   作者:  
最近需要用到預加載和延遲加載的東東,就參考寫了一個。支持跨頁面,支持超時設置與依賴設置。
復制代碼 代碼如下:

(function($) {
(function($) {
$.preload = function(data, cfg) {
return new Loader(data, cfg);
};
var maps = {}, on = $.event.add, un = $.event.remove, head = document.getElementsByTagName('head')[0], body =
document.body, bs = $.browser, ie = bs.msie, webkit = bs.webkit, gecko = bs.mozilla, space = 1000, ajax =
$.ajax,
loaders = $.preload.loaders = {
'js' : function(url, callback, timeout, defer) {
var s, timer;
if (defer) {
if (ie) {
return loaders.img(url, callback, timeout);
} else {
s = document.createElement('object');
s.data = url;
s.width = s.height = 0;
}
} else {
s = document.createElement('script');
s.setAttribute('type', 'text/javascript');
s.setAttribute('src', url);
}
function f() {
if (timer)
clearTimeout(timer);
s.onreadystatechange = s.onload = s.onerror = null;
callback(url, false);
}
if (ie) {
s.onreadystatechange = function() {
if (this.readyState === 'loaded' || this.readyState === 'complete') {
if (timer)
clearTimeout(timer);
s.onreadystatechange = null;
callback(url, true);
}
};
} else {
s.onload = function() {
if (timer)
clearTimeout(timer);
s.onload = s.onerror = null;
callback(url, true);
};
s.onerror = f;
}
timer = setTimeout(f, timeout);
body.appendChild(s);
},
'css' : function(url, callback, timeout, defer) {
if (defer) {
return loaders.js(url, callback, timeout, defer);
}
var s = document.createElement('link'), timer;
s.setAttribute('rel', 'stylesheet');
s.setAttribute('type', 'text/css');
s.setAttribute('href', url);
function f() {
if (timer)
clearTimeout(timer);
s.onreadystatechange = s.onload = s.onerror = null;
callback(url, false);
}
if (ie) {
s.onreadystatechange = function() {
if (this.readyState === 'loaded' || this.readyState === 'complete') {
if (timer)
clearTimeout(timer);
s.onreadystatechange = null;
callback(url, true);
}
};
timer = setTimeout(f, timeout);
} else if (webkit || gecko) {
timer = new Date();
function f() {
if (('sheet' in s) && ('cssRules' in s.sheet)) {
try {
callback(url, !!s.sheet.cssRules[0]);
} catch (e) {
setTimeout(f, space);
}
} else if (new Date() - timer > timeout) {
callback(url, false);
} else {
setTimeout(f, space);
}
}
setTimeout(f, space * 2);
} else {
s.onload = function() {
if (timer)
clearTimeout(timer);
s.onload = s.onerror = null;
callback(url, true);
};
s.onerror = f;
timer = setTimeout(f, timeout);
}
head.appendChild(s);
},
'img' : function(url, callback, timeout) {
var s = new Image(), timer;
function f() {
if (timer)
clearTimeout(timer);
s.onload = s.onerror = null;
callback(url, false);
}
s.onload = function() {
if (timer)
clearTimeout(timer);
s.onload = s.onerror = null;
callback(url, true);
};
s.onerror = f;
timer = setTimeout(f, timeout);
s.src = url;
},
'ajax' : function(url, callback, cfg) {
cfg = cfg || {};
cfg.url = url;
cfg.success = function(data) {
callback(url, true, data);
};
cfg.error = function() {
callback(url, false);
};
ajax(cfg);
}
};
function Loader(data, cfg) {
var self = this, cur = -1, items = [], pendings = [], done, i = 0, l = data.length, j, m, s, t, c, d, tt, item, doing =
0, load;
cfg = cfg || {};
for (; i < l; ++i) {
item = data[i];
if (typeof item === 'string') {
s = item.substr(item.lastIndexOf('.') + 1);
items.push(maps[item] = {
type : loaders[s] ? s : 'img',
url : item
});
} else if (item.urls) {
for (j = 0, s = item.type, t = item.require, c = item.callback, d = item.defer, tt = item.timeout, item =
item.urls, m = item.length; j < m; ++j) {
s = s || item[j].substr(item[j].lastIndexOf('.') + 1);
items.push(maps[item[j]] = {
type : loaders[s] ? s : 'img',
url : item[j],
require : t,
callback : c,
defer : d,
timeout : tt
});
}
} else {
if (!item.type) {
s = item.url.substr(item.url.lastIndexOf('.') + 1);
item.type = loaders[s] ? s : 'img';
}
items.push(maps[item.url] = item);
}
}
this.success = this.fail = this.progress = 0;
if (cfg.onFinish)
this.onFinish = cfg.onFinish;
timeout = cfg.timeout || 2000;
function callback(url, flag, data) {
if (flag) {
++self.success;
} else {
++self.fail;
}
self.progress = (self.success + self.fail) / items.length;
console.info(url);
console.warn(flag);
item = maps[url];
item.success = flag;
if (self.progress === 1) {
self.stop();
}
if (item.parent && !item.defer && !cfg.defer) {
$(item.parent)[0].innerHTML = data || '';
}
if (item.callback) {
item.callback(data);
}
item.done = true;
--doing;
}
function runnable(item, pend) {
var it;
if (typeof item.require === 'string') {
if (item.done)
return false;
if (!item.require)
return true;
it = maps[item.require];
if (!it || it.done) {
if (pend)
pendings.shift();
if (it && it.success) {
return true;
} else {
callback(item.url, false);
}
} else if (!pend) {
pendings.push(item);
}
} else {
for (it = item.length; it--;) {
if (!runnable(item[it], pend))
return false;
}
return true;
}
}
function run() {
var item = pendings[0];
if (!item || !runnable(item, true)) {
while (item = items[++cur]) {
if (runnable(item)) {
break;
}
}
}
if (item) {
var fn = loaders[item.type || 'img'];
if (fn) {
++doing;
if (item.type === 'ajax') {
if (item.cfg && !item.cfg.timeout)
item.cfg.timeout = timeout;
fn(item.url, callback, item.cfg);
} else {
fn(item.url, callback, item.timeout || timeout, item.defer === undefined ? cfg.defer
: item.defer);
}
};
if (load) {
run();
} else {
self.timer = setTimeout(run, space);
}
} else if (pendings.length) {
self.timer = setTimeout(run, space);
}
}
this.start = function(delay) {
if (!done)
this.timer = setTimeout(run, delay > space ? delay : space);
};
this.stop = function() {
if (this.timer) {
clearTimeout(this.timer);
this.timer = null;
done = true;
if (this.onFinish) {
if (!doing)
this.onFinish();
else {
s = setInterval(function() {
if (!doing) {
clearInterval(s);
self.onFinish();
}
}, space);
}
}
}
};
this.pause = function() {
clearTimeout(this.timer);
};
this.resume = function() {
this.timer = setTimeout(run, space);
};
this.load = function() {
clearTimeout(this.timer);
load = true;
run();
};
}
})(jQuery);
/**
* @example
* var loader = $.preload([
// 字符串,采用默認配置
'1.jpg', '1.js',
// 對象,自定義配置,如type, require, timeout, defer, callback
{
type : 'img',
url : 'http://foo.com/foo',
timeout : 10
}, {
url : '3.js',
callback : fn,
defer : true,
require : '1.js'
},
// 對象,可用urls指定一組相同配置
{
type : 'css',
urls : ['4.css', '5.css']
}], {
// 加載結束后調用
onFinish : fn,
// 加載超時
timeout : 50
});
// 開始預加載
loader.start();
loader.stop();
// 暫停預加載
loader.pause();
loader.resume();
// 實時加載
loader.load();
*/

相關文章

  • js獲取對象、數組的實際長度,元素實際個數的實現(xiàn)代碼

    js獲取對象、數組的實際長度,元素實際個數的實現(xiàn)代碼

    下面小編就為大家?guī)硪黄猨s獲取對象、數組的實際長度,元素實際個數的實現(xiàn)代碼。小編覺得挺不錯的,現(xiàn)在就分享 給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-06-06
  • js大數相加出現(xiàn)精度丟失、運算錯誤的問題

    js大數相加出現(xiàn)精度丟失、運算錯誤的問題

    js中數字類型長度達到16位時,進行加減乘除運算,會出現(xiàn)精度丟失,運算結果錯誤的問題,本文講述精度丟失的原因及解決辦法
    2023-08-08
  • JS事件循環(huán)機制event loop宏任務微任務原理解析

    JS事件循環(huán)機制event loop宏任務微任務原理解析

    這篇文章主要介紹了JS事件循環(huán)機制event loop宏任務微任務原理解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-08-08
  • JS實現(xiàn)仿PS的調色板效果完整實例

    JS實現(xiàn)仿PS的調色板效果完整實例

    這篇文章主要介紹了JS實現(xiàn)仿PS的調色板效果,結合完整實例形式分析了javascript通過運算與動態(tài)操作頁面元素實現(xiàn)調色板功能的相關操作技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2016-12-12
  • JavaScript實現(xiàn)酷炫的鼠標拖尾特效

    JavaScript實現(xiàn)酷炫的鼠標拖尾特效

    這篇文章主要為大家介紹了通過JavaScript實現(xiàn)的一個超級好看的鼠標拖尾特效,文中的示例代碼講解詳細,對我們學習JavaScript有一定的幫助,感興趣的可以學習一下
    2021-12-12
  • js實現(xiàn)盒子滾動動畫效果

    js實現(xiàn)盒子滾動動畫效果

    這篇文章主要為大家詳細介紹了js實現(xiàn)盒子滾動動畫效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-08-08
  • 每天一篇javascript學習小結(Date對象)

    每天一篇javascript學習小結(Date對象)

    這篇文章主要介紹了javascript中的Date對象知識點,對Date對象的基本使用方法,以及各種方法進行整理,感興趣的小伙伴們可以參考一下
    2015-11-11
  • 微信小程序BindTap快速連續(xù)點擊目標頁面跳轉多次問題處理

    微信小程序BindTap快速連續(xù)點擊目標頁面跳轉多次問題處理

    這篇文章主要介紹了微信小程序BindTap快速連續(xù)點擊目標頁面跳轉多次問題處理,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-04-04
  • 教您去掉ie網頁加載進度條的方法

    教您去掉ie網頁加載進度條的方法

    相信很多同仁做的系統(tǒng)后到都是用frameset或iframe來加載不同頁面的,不可不知道大家有沒有注意到,當frame框架中的頁面已經加載完成后,可是ie瀏覽器的狀態(tài)欄還會一直顯示一個正在加載的狀態(tài)。
    2010-12-12
  • Bootstrap Table中的多選框刪除功能

    Bootstrap Table中的多選框刪除功能

    這篇文章主要介紹了Bootstrap Table中的多選框刪除功能,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-07-07

最新評論

汉源县| 乌海市| 杭锦后旗| 东城区| 凤凰县| 夏河县| 祁东县| 龙海市| 永川市| 淳安县| 丁青县| 连城县| 安远县| 孟村| 潞西市| 德州市| 靖边县| 武清区| 沭阳县| 福安市| 阿瓦提县| 通渭县| 突泉县| 湘阴县| 静宁县| 行唐县| 上高县| 铜陵市| 玛曲县| 新密市| 云龙县| 安福县| 乌海市| 崇礼县| 左贡县| 繁峙县| 本溪| 新乐市| 高安市| 阿克苏市| 清远市|