javascript圖片預先載入完整執行個體_javascript技巧

來源:互聯網
上載者:User

本文執行個體講述了javascript圖片預先載入的方法。分享給大家供大家參考,具體如下:

<!DOCTYPE ><html xmlns="http://www.w3.org/1999/xhtml"><head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>圖片預先載入</title></head><body><img id="song" width="300" src="yun_qi_img/bdlogo.gif" _src="yun_qi_img/middlexznxwkia.gif" /></body></html><script type="text/javascript"> //預設圖片 var defurl = 'http://www.baidu.com/img/bdlogo.gif'; //一張大尺寸圖片 var imgurl = 'yun_qi_img/vistas24.jpg'; //一張gif圖片 var gifurl = 'http://p2009c.zbjimg.com/task/2009-12/03/188273/middlexznxwkia.gif'; //圖片預先載入 function loadImage(url, callback) {  var img = new Image(); //建立一個Image對象,實現圖片的預下載  img.onload = function(){   img.onload = null;//gif圖片在ie下會迴圈請求   callback(img);  }  img.src = url; } var img = document.getElementById('song'); var url = img.getAttribute('_src') loadImage(gifurl,function(){  //alert('ok');  img.src = url; }) /**  * 網路上通用的圖片預先載入函數  * @param url  * @param callback  */ var imgLoad = function (url, callback) {  var img = new Image();  img.src = url;  if (img.complete) {   callback(img.width, img.height);  } else {   img.onload = function () {    callback(img.width, img.height);    img.onload = null;   };  }; }; // 更新: // 05.27: 1、保證回調執行順序:error > ready > load;2、回呼函數this指向img本身 // 04-02: 1、增加圖片完全載入後的回調 2、提高效能 /**  * 圖標題資料載入就緒事件 - 更快擷取圖片尺寸  *  * 原理:沒有緩衝的情況下,用計時器不斷驗證圖片的大小是否發生變化,如果不在變化則為ready  *  如果有緩衝則w3c瀏覽器會調用 complete,ie則會走 onload,都不在走 計時器那部分  *  * @version 2011.05.27  * @author TangBin  * @param {String} 圖片路徑  * @param {Function} 尺寸就緒  * @param {Function} 載入完畢 (可選)  * @param {Function} 載入錯誤 (可選)  * @example imgReady('yun_qi_img/logo_cn.png', function () {alert('size ready: width=' + this.width + '; height=' + this.height);});  */ var imgReady = (function () {  var list = [], intervalId = null,   // 用來執行隊列   tick = function () {    var i = 0;    for (; i < list.length; i++) {     list[i].end ? list.splice(i--, 1) : list[i]();    }    ;    !list.length && stop();   },   // 停止所有定時器隊列   stop = function () {    clearInterval(intervalId);    intervalId = null;   };  return function (url, ready, load, error) {   var onready, width, height, newWidth, newHeight,    img = new Image();   img.src = url;   // 如果圖片被緩衝,則直接返回快取資料   if (img.complete) {    ready.call(img);    load && load.call(img);    return;   }   ;   width = img.width;   height = img.height;   // 載入錯誤後的事件   img.onerror = function () {    error && error.call(img);    onready.end = true;    img = img.onload = img.onerror = null;   };   // 圖片尺寸就緒   onready = function () {    newWidth = img.width;    newHeight = img.height;    if (newWidth !== width || newHeight !== height ||     // 如果圖片已經在其他地方載入可使用面積檢測     newWidth * newHeight > 1024     ) {     ready.call(img);     onready.end = true;    }    ;   };   onready();   // 完全載入完畢的事件   img.onload = function () {     // onload在定時器時間差範圍內可能比onready快    // 這裡進行檢查並保證onready優先執行    !onready.end && onready();    load && load.call(img);    // IE gif動畫會迴圈執行onload,置空onload即可    img = img.onload = img.onerror = null;   };    // 排入佇列中定期執行   if (!onready.end) {    list.push(onready);    // 無論何時只允許出現一個定時器,減少瀏覽器效能損耗    if (intervalId === null) intervalId = setInterval(tick, 40);   }   ;  }; })(); var img_url = 'yun_qi_img/vistas24.jpg'; imgReady(img_url, function () {  //console.info(this.width);  //console.info(this.height);  alert(this.width + '\n' + this.height);  document.getElementById('song').src = img_url; })</script>

希望本文所述對大家JavaScript程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.