首先,你需要獲取圖像的屬性,看看哪一個更大,高度或寬度,然後調整基礎上,格大小圖像的窗口大小調整後,像這樣:
//this would load up initially, you will need this data for future processing
div = $("div");
imgsrc = div.find('img').attr('src');
var img = new Image()
img.onload = function(){
imgw = img.width;
imgh = img.height;
//after its loaded and you got the size..
//you need to call it the first time of course here and make it visible:
resizeMyImg(imgw,imgh);
div.find('img').show();
//now you can use your resize function
$(window).resize(function(){ resizeMyImg(imgw,imgh) });
}
img.src = imgsrc
function resizeMyImg(w,h){
//the width is larger
if (w > h) {
//resize the image to the div
div.find('img').width(div.innerWidth() + 'px').height('auto');
}
else {
// the height is larger or the same
//resize the image to the div
div.find('img').height(div.innerHeight() + 'px').width('auto');
}
}
你實際上可以在這裏找到一個完整的解決方案: http://jsfiddle.net/CDywS/1
是的!謝謝! – r0tterz