首先需要获取图片属性,看哪个大,高还是宽,然后在窗口调整大小后根据div大小调整图片大小,像这样:
//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