基于jquery的防止大圖片撐破頁面的實(shí)現(xiàn)代碼(立即縮放)
更新時(shí)間:2011年10月24日 01:24:51 作者:
這篇文章將根據(jù)此寫一個(gè)應(yīng)用:讓圖片未加載完畢就實(shí)現(xiàn)按比例自適應(yīng),防止圖片撐破布局(尤其是外鏈圖片)
為了防止圖片撐破布局,最常見的仍然是通過onload后獲取圖片尺寸再進(jìn)行調(diào)整,所以加載過程中仍然會(huì)撐破。而Qzone日志的圖片在此進(jìn)行了改進(jìn),onload完畢后才顯示原圖。我以前用onload寫過一個(gè)小例子:http://www.planeart.cn/?p=1022
通過imgReady可以跨瀏覽器在dom ready就可以實(shí)現(xiàn)圖片自適應(yīng),無需等待img加載,代碼如下:
(3-17修復(fù)網(wǎng)友crossyou 指出的一處錯(cuò)誤,并且新版本去掉了替換圖片)
// jquery.autoIMG.js - 2010-04-02 - Tang Bin - http://planeArt.cn/ - MIT Licensed
(function ($) {
// 檢測是否支持css2.1 max-width屬性
var isMaxWidth = 'maxWidth' in document.documentElement.style,
// 檢測是否IE7瀏覽器
isIE7 = !-[1,] && !('prototype' in Image) && isMaxWidth;
$.fn.autoIMG = function () {
var maxWidth = this.width();
return this.find('img').each(function (i, img) {
// 如果支持max-width屬性則使用此,否則使用下面方式
if (isMaxWidth) return img.style.maxWidth = maxWidth + 'px';
var src = img.src;
// 隱藏原圖
img.style.display = 'none';
img.removeAttribute('src');
// 獲取圖片頭尺寸數(shù)據(jù)后立即調(diào)整圖片
imgReady(src, function (width, height) {
// 等比例縮小
if (width > maxWidth) {
height = maxWidth / width * height,
width = maxWidth;
img.style.width = width + 'px';
img.style.height = height + 'px';
};
// 顯示原圖
img.style.display = '';
img.setAttribute('src', src);
});
});
};
// IE7縮放圖片會(huì)失真,采用私有屬性通過三次插值解決
isIE7 && (function (c,d,s) {s=d.createElement('style');d.getElementsByTagName('head')[0].appendChild(s);s.styleSheet&&(s.styleSheet.cssText+=c)||s.appendChild(d.createTextNode(c))})('img {-ms-interpolation-mode:bicubic}',document);
/**
* 圖片頭數(shù)據(jù)加載就緒事件
* @see http://www.planeart.cn/?p=1121
* @param {String} 圖片路徑
* @param {Function} 尺寸就緒 (參數(shù)1接收width; 參數(shù)2接收height)
* @param {Function} 加載完畢 (可選. 參數(shù)1接收width; 參數(shù)2接收height)
* @param {Function} 加載錯(cuò)誤 (可選)
*/
var imgReady = (function () {
var list = [], intervalId = null,
// 用來執(zhí)行隊(duì)列
tick = function () {
var i = 0;
for (; i < list.length; i++) {
list[i].end ? list.splice(i--, 1) : list[i]();
};
!list.length && stop();
},
// 停止所有定時(shí)器隊(duì)列
stop = function () {
clearInterval(intervalId);
intervalId = null;
};
return function (url, ready, load, error) {
var check, width, height, newWidth, newHeight,
img = new Image();
img.src = url;
// 如果圖片被緩存,則直接返回緩存數(shù)據(jù)
if (img.complete) {
ready(img.width, img.height);
load && load(img.width, img.height);
return;
};
// 檢測圖片大小的改變
width = img.width;
height = img.height;
check = function () {
newWidth = img.width;
newHeight = img.height;
if (newWidth !== width || newHeight !== height ||
// 如果圖片已經(jīng)在其他地方加載可使用面積檢測
newWidth * newHeight > 1024
) {
ready(newWidth, newHeight);
check.end = true;
};
};
check();
// 加載錯(cuò)誤后的事件
img.onerror = function () {
error && error();
check.end = true;
img = img.onload = img.onerror = null;
};
// 完全加載完畢的事件
img.onload = function () {
load && load(img.width, img.height);
!check.end && check();
// IE gif動(dòng)畫會(huì)循環(huán)執(zhí)行onload,置空onload即可
img = img.onload = img.onerror = null;
};
// 加入隊(duì)列中定期執(zhí)行
if (!check.end) {
list.push(check);
// 無論何時(shí)只允許出現(xiàn)一個(gè)定時(shí)器,減少瀏覽器性能損耗
if (intervalId === null) intervalId = setInterval(tick, 40);
};
};
})();
})(jQuery);
autoIMG壓縮后:1.74kb,兼容:Chrome | Firefox | Sifari | Opera | IE6 | IE7 | IE8 | …
調(diào)用演示:$(‘#demo p').autoIMG()
同樣,令人愉悅的DEMO地址在這里:http://demo.jb51.net/js/2011/autoimg/
后記:雖然有了上一篇文章imgReady技術(shù)的鋪墊,我以為會(huì)很簡單的實(shí)現(xiàn)這個(gè)圖片自適應(yīng)插件,可是過程中卻在webkit內(nèi)核的瀏覽器碰了一鼻子灰,后來才得知webkit有BUG未修復(fù),webkit無法無法中斷img下載。我折騰許久后更新了imgReady函數(shù)與這個(gè)例子。
打包下載地址
通過imgReady可以跨瀏覽器在dom ready就可以實(shí)現(xiàn)圖片自適應(yīng),無需等待img加載,代碼如下:
(3-17修復(fù)網(wǎng)友crossyou 指出的一處錯(cuò)誤,并且新版本去掉了替換圖片)
復(fù)制代碼 代碼如下:
// jquery.autoIMG.js - 2010-04-02 - Tang Bin - http://planeArt.cn/ - MIT Licensed
(function ($) {
// 檢測是否支持css2.1 max-width屬性
var isMaxWidth = 'maxWidth' in document.documentElement.style,
// 檢測是否IE7瀏覽器
isIE7 = !-[1,] && !('prototype' in Image) && isMaxWidth;
$.fn.autoIMG = function () {
var maxWidth = this.width();
return this.find('img').each(function (i, img) {
// 如果支持max-width屬性則使用此,否則使用下面方式
if (isMaxWidth) return img.style.maxWidth = maxWidth + 'px';
var src = img.src;
// 隱藏原圖
img.style.display = 'none';
img.removeAttribute('src');
// 獲取圖片頭尺寸數(shù)據(jù)后立即調(diào)整圖片
imgReady(src, function (width, height) {
// 等比例縮小
if (width > maxWidth) {
height = maxWidth / width * height,
width = maxWidth;
img.style.width = width + 'px';
img.style.height = height + 'px';
};
// 顯示原圖
img.style.display = '';
img.setAttribute('src', src);
});
});
};
// IE7縮放圖片會(huì)失真,采用私有屬性通過三次插值解決
isIE7 && (function (c,d,s) {s=d.createElement('style');d.getElementsByTagName('head')[0].appendChild(s);s.styleSheet&&(s.styleSheet.cssText+=c)||s.appendChild(d.createTextNode(c))})('img {-ms-interpolation-mode:bicubic}',document);
/**
* 圖片頭數(shù)據(jù)加載就緒事件
* @see http://www.planeart.cn/?p=1121
* @param {String} 圖片路徑
* @param {Function} 尺寸就緒 (參數(shù)1接收width; 參數(shù)2接收height)
* @param {Function} 加載完畢 (可選. 參數(shù)1接收width; 參數(shù)2接收height)
* @param {Function} 加載錯(cuò)誤 (可選)
*/
var imgReady = (function () {
var list = [], intervalId = null,
// 用來執(zhí)行隊(duì)列
tick = function () {
var i = 0;
for (; i < list.length; i++) {
list[i].end ? list.splice(i--, 1) : list[i]();
};
!list.length && stop();
},
// 停止所有定時(shí)器隊(duì)列
stop = function () {
clearInterval(intervalId);
intervalId = null;
};
return function (url, ready, load, error) {
var check, width, height, newWidth, newHeight,
img = new Image();
img.src = url;
// 如果圖片被緩存,則直接返回緩存數(shù)據(jù)
if (img.complete) {
ready(img.width, img.height);
load && load(img.width, img.height);
return;
};
// 檢測圖片大小的改變
width = img.width;
height = img.height;
check = function () {
newWidth = img.width;
newHeight = img.height;
if (newWidth !== width || newHeight !== height ||
// 如果圖片已經(jīng)在其他地方加載可使用面積檢測
newWidth * newHeight > 1024
) {
ready(newWidth, newHeight);
check.end = true;
};
};
check();
// 加載錯(cuò)誤后的事件
img.onerror = function () {
error && error();
check.end = true;
img = img.onload = img.onerror = null;
};
// 完全加載完畢的事件
img.onload = function () {
load && load(img.width, img.height);
!check.end && check();
// IE gif動(dòng)畫會(huì)循環(huán)執(zhí)行onload,置空onload即可
img = img.onload = img.onerror = null;
};
// 加入隊(duì)列中定期執(zhí)行
if (!check.end) {
list.push(check);
// 無論何時(shí)只允許出現(xiàn)一個(gè)定時(shí)器,減少瀏覽器性能損耗
if (intervalId === null) intervalId = setInterval(tick, 40);
};
};
})();
})(jQuery);
autoIMG壓縮后:1.74kb,兼容:Chrome | Firefox | Sifari | Opera | IE6 | IE7 | IE8 | …
調(diào)用演示:$(‘#demo p').autoIMG()
同樣,令人愉悅的DEMO地址在這里:http://demo.jb51.net/js/2011/autoimg/
后記:雖然有了上一篇文章imgReady技術(shù)的鋪墊,我以為會(huì)很簡單的實(shí)現(xiàn)這個(gè)圖片自適應(yīng)插件,可是過程中卻在webkit內(nèi)核的瀏覽器碰了一鼻子灰,后來才得知webkit有BUG未修復(fù),webkit無法無法中斷img下載。我折騰許久后更新了imgReady函數(shù)與這個(gè)例子。
打包下載地址
您可能感興趣的文章:
- jQuery實(shí)現(xiàn)等比例縮放大圖片讓大圖片自適應(yīng)頁面布局
- jQuery+css實(shí)現(xiàn)的點(diǎn)擊圖片放大縮小預(yù)覽功能示例【圖片預(yù)覽 查看大圖】
- jQuery實(shí)現(xiàn)鼠標(biāo)滑過商品小圖片上顯示對應(yīng)大圖片功能【測試可用】
- jQuery實(shí)現(xiàn)鼠標(biāo)滑過預(yù)覽圖片大圖效果的方法
- jQuery實(shí)現(xiàn)大圖輪播
- jQuery實(shí)現(xiàn)的小圖列表,大圖展示效果幻燈片示例
- jquery插件jquery.LightBox.js實(shí)現(xiàn)點(diǎn)擊放大圖片并左右點(diǎn)擊切換效果(附demo源碼下載)
- jQuery實(shí)現(xiàn)點(diǎn)擊小圖片淡入淡出顯示大圖片特效
- jQuery實(shí)現(xiàn)點(diǎn)擊查看大圖并以彈框的形式居中
- 基于jQuery插件實(shí)現(xiàn)點(diǎn)擊小圖顯示大圖效果
- jquery實(shí)現(xiàn)移動(dòng)端點(diǎn)擊圖片查看大圖特效
- jQuery實(shí)現(xiàn)點(diǎn)擊小圖顯示大圖代碼分享
- jquery 圖片點(diǎn)擊放大預(yù)覽功能詳解
相關(guān)文章
jquery 提示信息顯示后自動(dòng)消失的具體實(shí)現(xiàn)
讓提示信息顯示后自動(dòng)消失的方法有很多,在本文為大家介紹下使用jquery是如何做到的,感興趣朋友可以參考下2013-12-12
jQuery EasyUI API 中文文檔 - Parser 解析器
jQuery EasyUI API 中文文檔 - Parser 解析器,使用jQuery EasyUI的朋友可以參考下。2011-09-09
jQuery實(shí)現(xiàn)移動(dòng)端扭蛋機(jī)抽獎(jiǎng)
這篇文章主要為大家詳細(xì)介紹了jQuery實(shí)現(xiàn)移動(dòng)端扭蛋機(jī)抽獎(jiǎng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-11-11
jquery radio的取值_radio的選中_radio的重置方法
下面小編就為大家?guī)硪黄猨query radio的取值_radio的選中_radio的重置方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-09-09
防止jQuery ajax Load使用緩存的方法小結(jié)
本篇文章主要是對防止jQuery ajax Load使用緩存的方法進(jìn)行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助2014-02-02
使用prop解決一個(gè)checkbox選中后再次選中失效的問題
下面小編就為大家?guī)硪黄褂胮rop解決一個(gè)checkbox選中后再次選中失效的問題。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-07-07
jQuery插件jFade實(shí)現(xiàn)鼠標(biāo)經(jīng)過的圖片高亮其它變暗
本文給大家介紹的是一款重點(diǎn)突出的jQuery特效插件效果,使用jFade實(shí)現(xiàn)鼠標(biāo)經(jīng)過的圖片高亮其它變暗,非常實(shí)用,推薦給小伙伴們參考下。2015-03-03
20款非常優(yōu)秀的 jQuery 工具提示插件 推薦
工具提示(Tooltip)在網(wǎng)站中的一個(gè)小功能,但卻有很重要的作用,常用于顯示一些溫馨的提示信息。如果網(wǎng)站中的工具提示功能做得非常有創(chuàng)意的話能夠加深用戶對網(wǎng)站印象2012-07-07

