基于jquery的防止大圖片撐破頁(yè)面的實(shí)現(xiàn)代碼(立即縮放)
更新時(shí)間:2011年10月24日 01:24:51 作者:
這篇文章將根據(jù)此寫(xiě)一個(gè)應(yīng)用:讓圖片未加載完畢就實(shí)現(xiàn)按比例自適應(yīng),防止圖片撐破布局(尤其是外鏈圖片)
為了防止圖片撐破布局,最常見(jiàn)的仍然是通過(guò)onload后獲取圖片尺寸再進(jìn)行調(diào)整,所以加載過(guò)程中仍然會(huì)撐破。而Qzone日志的圖片在此進(jìn)行了改進(jìn),onload完畢后才顯示原圖。我以前用onload寫(xiě)過(guò)一個(gè)小例子:http://www.planeart.cn/?p=1022
通過(guò)imgReady可以跨瀏覽器在dom ready就可以實(shí)現(xiàn)圖片自適應(yīng),無(wú)需等待img加載,代碼如下:
(3-17修復(fù)網(wǎng)友crossyou 指出的一處錯(cuò)誤,并且新版本去掉了替換圖片)
// jquery.autoIMG.js - 2010-04-02 - Tang Bin - http://planeArt.cn/ - MIT Licensed
(function ($) {
// 檢測(cè)是否支持css2.1 max-width屬性
var isMaxWidth = 'maxWidth' in document.documentElement.style,
// 檢測(cè)是否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ì)失真,采用私有屬性通過(guò)三次插值解決
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,
// 用來(lái)執(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;
};
// 檢測(cè)圖片大小的改變
width = img.width;
height = img.height;
check = function () {
newWidth = img.width;
newHeight = img.height;
if (newWidth !== width || newHeight !== height ||
// 如果圖片已經(jīng)在其他地方加載可使用面積檢測(cè)
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ì)循環(huán)執(zhí)行onload,置空onload即可
img = img.onload = img.onerror = null;
};
// 加入隊(duì)列中定期執(zhí)行
if (!check.end) {
list.push(check);
// 無(wú)論何時(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ì)很簡(jiǎn)單的實(shí)現(xiàn)這個(gè)圖片自適應(yīng)插件,可是過(guò)程中卻在webkit內(nèi)核的瀏覽器碰了一鼻子灰,后來(lái)才得知webkit有BUG未修復(fù),webkit無(wú)法無(wú)法中斷img下載。我折騰許久后更新了imgReady函數(shù)與這個(gè)例子。
打包下載地址
通過(guò)imgReady可以跨瀏覽器在dom ready就可以實(shí)現(xiàn)圖片自適應(yīng),無(wú)需等待img加載,代碼如下:
(3-17修復(fù)網(wǎng)友crossyou 指出的一處錯(cuò)誤,并且新版本去掉了替換圖片)
復(fù)制代碼 代碼如下:
// jquery.autoIMG.js - 2010-04-02 - Tang Bin - http://planeArt.cn/ - MIT Licensed
(function ($) {
// 檢測(cè)是否支持css2.1 max-width屬性
var isMaxWidth = 'maxWidth' in document.documentElement.style,
// 檢測(cè)是否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ì)失真,采用私有屬性通過(guò)三次插值解決
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,
// 用來(lái)執(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;
};
// 檢測(cè)圖片大小的改變
width = img.width;
height = img.height;
check = function () {
newWidth = img.width;
newHeight = img.height;
if (newWidth !== width || newHeight !== height ||
// 如果圖片已經(jīng)在其他地方加載可使用面積檢測(cè)
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ì)循環(huán)執(zhí)行onload,置空onload即可
img = img.onload = img.onerror = null;
};
// 加入隊(duì)列中定期執(zhí)行
if (!check.end) {
list.push(check);
// 無(wú)論何時(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ì)很簡(jiǎn)單的實(shí)現(xiàn)這個(gè)圖片自適應(yīng)插件,可是過(guò)程中卻在webkit內(nèi)核的瀏覽器碰了一鼻子灰,后來(lái)才得知webkit有BUG未修復(fù),webkit無(wú)法無(wú)法中斷img下載。我折騰許久后更新了imgReady函數(shù)與這個(gè)例子。
打包下載地址
您可能感興趣的文章:
- jQuery實(shí)現(xiàn)等比例縮放大圖片讓大圖片自適應(yīng)頁(yè)面布局
- jQuery+css實(shí)現(xiàn)的點(diǎn)擊圖片放大縮小預(yù)覽功能示例【圖片預(yù)覽 查看大圖】
- jQuery實(shí)現(xiàn)鼠標(biāo)滑過(guò)商品小圖片上顯示對(duì)應(yīng)大圖片功能【測(cè)試可用】
- jQuery實(shí)現(xiàn)鼠標(biāo)滑過(guò)預(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)文章
推薦40個(gè)非常優(yōu)秀的jQuery插件和教程【系列三】
jQuery 在如今的 Web 開(kāi)發(fā)項(xiàng)目中扮演著重要角色,jQuery 以其插件眾多、獨(dú)特、輕量以及支持大規(guī)模的網(wǎng)站開(kāi)發(fā)聞名。本文大家分享40個(gè)實(shí)用的 jQuery 插件,可以根據(jù)您的項(xiàng)目需要來(lái)選擇使用2011-11-11JQuery+drag.js上傳圖片并且實(shí)現(xiàn)圖片拖曳
這篇文章主要介紹了JQuery+drag.js上傳圖片并且實(shí)現(xiàn)圖片拖曳,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11基于JQuery的模擬蘋(píng)果桌面Dock效果(穩(wěn)定版)
之所以將它命名為穩(wěn)定版,是因?yàn)橹耙呀?jīng)分享了一個(gè)初級(jí)版本的,之前的初級(jí)版中存在很多bug?,F(xiàn)在經(jīng)過(guò)反復(fù)琢磨、實(shí)驗(yàn),修復(fù)了之前版本存在的很多不足之處,就算鼠標(biāo)快速的滑動(dòng)依然表現(xiàn)的很穩(wěn)定2012-10-10jQuery代碼實(shí)現(xiàn)實(shí)時(shí)獲取時(shí)間
這篇文章主要介紹了jQuery代碼實(shí)現(xiàn)實(shí)時(shí)獲取時(shí)間功能,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2017-01-01JQuery一種取同級(jí)值的方式(比如你在GridView中)
JQuery一種取同級(jí)值的方式 比如你在GridView中,實(shí)現(xiàn)代碼如下,需要的朋友可以參考下2012-03-03解決Jquery鼠標(biāo)經(jīng)過(guò)不?;瑒?dòng)的問(wèn)題
在鼠標(biāo)經(jīng)過(guò)的時(shí)候不停的顯示隱藏html元素,正確的寫(xiě)法應(yīng)該是下面這樣的,需要的朋友可以參考下2014-03-03