我正在尝试将图像拉伸到div中.但是我希望它溢出,以便填满整个div. div使用页面调整大小.我想要一个类似于jQuery bgstretcher插件(
http://www.ajaxblender.com/bgstretcher-2-jquery-stretch-background-plugin-updated.html)的效果,但是我不能使用该插件,因为我已经将它用于页面上的背景图像,当你尝试激活它的多个实例时,它似乎不起作用.
所以我要问的是某种简单的jQuery函数,它调整图像的大小以完全填充隐藏溢出的div,这样就没有间隙,但不会使图像偏斜.
编辑:
http://jsfiddle.net/R7UCZ/
这就是我所追求的,但我希望图像能够填满整个div,而不会产生偏差.如果图像有点偏离div,那很好,但是我需要用div来调整大小,div会调整页面的高度和宽度.
解决方法
首先,您需要获取图像属性并查看哪个更大,高度或宽度,然后在窗口调整大小后根据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