日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
再談JavaScript圖片預(yù)加載技術(shù)

推薦專題:JavaScript學(xué)習(xí)筆記 有問有答

創(chuàng)新互聯(lián)主打移動(dòng)網(wǎng)站、成都網(wǎng)站制作、成都網(wǎng)站建設(shè)、外貿(mào)營銷網(wǎng)站建設(shè)、網(wǎng)站改版、網(wǎng)絡(luò)推廣、網(wǎng)站維護(hù)、域名與空間、等互聯(lián)網(wǎng)信息服務(wù),為各行業(yè)提供服務(wù)。在技術(shù)實(shí)力的保障下,我們?yōu)榭蛻舫兄Z穩(wěn)定,放心的服務(wù),根據(jù)網(wǎng)站的內(nèi)容與功能再?zèng)Q定采用什么樣的設(shè)計(jì)。最后,要實(shí)現(xiàn)符合網(wǎng)站需求的內(nèi)容、功能與設(shè)計(jì),我們還會(huì)規(guī)劃穩(wěn)定安全的技術(shù)方案做保障。

一段典型的使用預(yù)加載獲取圖片大小的例子:

 
 
 
  1. var imgLoad = function (url, callback) {  
  2. var img = new Image();  
  3.  
  4. img.src = url;  
  5. if (img.complete) {  
  6. callback(img.width, img.height);  
  7. } else {  
  8. img.onload = function () {  
  9. callback(img.width, img.height);  
  10. img.onload = null;  
  11. };  
  12. };  
  13.  
  14. }; 

可以看到使用onload的方式必須等待圖片加載完畢,其速度不敢恭維。

web應(yīng)用程序區(qū)別于桌面應(yīng)用程序,響應(yīng)速度才是最好的用戶體驗(yàn)。如果想要速度與優(yōu)雅兼得,那就必須提前獲得圖片尺寸,如何在圖片沒有加載完畢就能獲取圖片尺寸?

十多年的上網(wǎng)經(jīng)驗(yàn)告訴我:瀏覽器在加載圖片的時(shí)候你會(huì)看到圖片會(huì)先占用一塊地然后才慢慢加載完畢,并且這里大部分的圖片都是沒有預(yù)設(shè)width與height屬性的,因?yàn)闉g覽器能夠獲取圖片的頭部數(shù)據(jù)?;诖?,只需要使用javascript定時(shí)偵測(cè)圖片的尺寸狀態(tài)便可得知圖片尺寸就緒的狀態(tài)。

實(shí)現(xiàn)代碼:

 
 
 
  1. var imgReady = function (url, callback, error) {  
  2. var width, height, intervalId, check, div,  
  3. img = new Image(),  
  4. body = document.body;  
  5.  
  6. img.src = url;  
  7.  
  8. // 從緩存中讀取  
  9. if (img.complete) {  
  10. return callback(img.width, img.height);  
  11. };  
  12.  
  13. // 通過占位提前獲取圖片頭部數(shù)據(jù)  
  14. if (body) {  
  15. div = document.createElement('div');  
  16. div.style.cssText = 'visibility:hidden;position:absolute;left:0;top:0;width:1px;height:1px;overflow:hidden';  
  17. div.appendChild(img)  
  18. body.appendChild(div);  
  19. width = img.offsetWidth;  
  20. height = img.offsetHeight;  
  21.  
  22. check = function () {  
  23. if (img.offsetWidth !== width || img.offsetHeight !== height) {  
  24. clearInterval(intervalId);  
  25. callback(img.offsetWidth, img.clientHeight);  
  26. img.onload = null;  
  27. div.innerHTML = '';  
  28. div.parentNode.removeChild(div);  
  29. };  
  30. };  
  31.  
  32. intervalId = setInterval(check, 150);  
  33. };  
  34.  
  35. // 加載完畢后方式獲取  
  36. img.onload = function () {  
  37. callback(img.width, img.height);  
  38. img.onload = img.onerror = null;  
  39. clearInterval(intervalId);  
  40. body && img.parentNode.removeChild(img);  
  41. };  
  42.  
  43. // 圖片加載錯(cuò)誤  
  44. img.onerror = function () {  
  45. error && error();  
  46. clearInterval(intervalId);  
  47. body && img.parentNode.removeChild(img);  
  48. };  
  49.  
  50. }; 

是不是很簡單?這樣的方式獲取攝影級(jí)別照片尺寸的速度往往是onload方式的幾十多倍,而對(duì)于web普通(800×600內(nèi))瀏覽級(jí)別的圖片能達(dá)到秒殺效果。

好了,請(qǐng)觀賞令人愉悅的 DEMO ,通過測(cè)試的瀏覽器:Chrome、Firefox、Safari、Opera、IE6、IE7、IE8。

原文鏈接:http://www.planeart.cn/?p=1121


網(wǎng)頁題目:再談JavaScript圖片預(yù)加載技術(shù)
網(wǎng)頁路徑:http://m.5511xx.com/article/copjpos.html