1
我需要使圖像適合視口/窗口大小。使圖像適合視口
我用這個代碼做:
$(".fittedImg").each(function() {
// I want to limit the size, and show a scrollbar on very small viewport
height = ($(window).height() < 725) ? 725 : $(window).height();
width = ($(window).width() < 1150) ? 1150 : $(window).width();
var newWidth, newHeight;
$(this)
.removeAttr("height")
.removeAttr("width"); // reset img size
// calculate dimension and keep it
if (($(this).width()/width) > ($(this).height()/height)) {
newHeight = height;
newWidth = height/($(this).height()/$(this).width());
} else {
newHeight = width/($(this).width()/$(this).height());
newWidth = width;
};
$(this).stop(true, false).animate({
"height": newHeight + "px",
"width": newWidth + "px"
});
});
我裹在我上調整大小調用一個函數的代碼,並在活動,如$(「fittedImg。」)load()和$。 ( '.fittedImg')。就緒()。
結果非常奇怪: 在Chrome中,它通常有效,但有時圖像會在重複刷新時延長。在IE瀏覽器上也有相同的問題。歌劇不會永遠不會失敗,Safari會忽略計算(永遠不會延伸)。
我的計算和/或事件調用有什麼問題?
謝謝。
1.我該怎麼做? load()和ready()不夠好? 2.沒有幫助。 – SRachamim 2012-03-06 10:47:24
看我的例子作爲樣板。 '.load()'應該足以獲得寬度/高度,但是您可以在處理程序中調整值以確保。 – David 2012-03-06 10:50:13