javaScript圖片預先載入後讓圖片像素變清晰

來源:互聯網
上載者:User

3. 當用戶端請求該圖時,4M的原圖肯定不會馬上下好,特別是遠程網路,這時先把小圖下過來,並拉升到原圖大小(這個操作應該是採用無插值演算法的直接放大,速度是很快的。)


應該是儲存了好幾張不同尺寸不同解析度的多張圖片,當大圖片沒載入完之前先逐次顯示小圖片,最後把顯示大圖片

 代碼如下 複製代碼

<img id="myImage" src="small_1.jpg" width="200" />
var img = new Image();
img.src = "yun_qi_img/big_800_600.jpg";
img.onload = function() {
document.getElementById('myImage').src = this.src;
}


javascript圖片預先載入技術

lightbox類效果為了讓圖片置中顯示而使用預先載入,需要等待完全載入完畢才能顯示,體驗不佳(如filick相簿的全屏效果)。javascript無法擷取img檔案頭資料,真的是這樣嗎?本文通過一個巧妙的方法讓javascript擷取它。

這是大部分人使用預先載入擷取圖片大小的例子:


當然實際中會有一些相容陷阱,如width與height檢測各個瀏覽器的不一致,還有webkit new Image()建立的圖片會受以處在載入進程中同url圖片影響,經過反覆測試後的最佳處理方式:

 

 代碼如下 複製代碼

// 更新:

// 05.27: 1、保證回調執行順序:error > ready > load;2、回呼函數this指向img本身

// 04-02: 1、增加圖片完全載入後的回調 2、提高效能

 

/**

* 圖標題資料載入就緒事件 - 更快擷取圖片尺寸

* @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);

};

};

})();

調用例子:

 

 代碼如下 複製代碼

imgReady('yun_qi_img/logo_cn.png', function () {

alert('size ready: width=' + this.width + '; height=' + this.height);

});

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.