7

我有高度和宽度不同的 div,并希望我的图像能够自动调整大小以 100% 填充这些 div,然后当然是居中。

目前我的图像设置为宽度 100%,然后使用下面居中的 jQuery,但这仅适用于高度超过 div 的图像,一旦调整大小.. 我如何使其高度和宽度都为 100% 和中心也.. 完全填充 div(即使这意味着拉伸图像)!

谢谢。

$('img.shelf-img').each(function(i, item) {
    var img_height = $(item).height();
    var top_margin = -(img_height / 2);
    $(item).css({
        'top': '50%',
        'margin-top': top_margin
    });
});
4

2 回答 2

16

使用 CSS 将图像的宽度和高度都设置为 100%,图像将自动拉伸以填充包含的 div,而不需要 jquery。

此外,您不需要将图像居中,因为它已经被拉伸以填充 div(以零边距居中)。

HTML:

<div id="containingDiv">
    <img src="">
</div>

CSS:

#containingDiv{
    width: 200px;
    height: 100px;
}
#containingDiv img{
    width: 100%;
    height: 100%;
}

That way, if your users have javascript disabled, the image will still be stretched to fill the entire div width/height.

OR

The JQuery way (SHRINK/STRETCH TO FIT - INCLUDES WHITESPACE):

$('img.shelf-img').each(function(i, item) {
    var img_height = $(item).height();
    var div_height = $(item).parent().height();
    if(img_height<div_height){
        //IMAGE IS SHORTER THAN CONTAINER HEIGHT - CENTER IT VERTICALLY
        var newMargin = (div_height-img_height)/2+'px';
        $(item).css({'margin-top': newMargin });
    }else if(img_height>div_height){
        //IMAGE IS GREATER THAN CONTAINER HEIGHT - REDUCE HEIGHT TO CONTAINER MAX - SET WIDTH TO AUTO  
        $(item).css({'width': 'auto', 'height': '100%'});
        //CENTER IT HORIZONTALLY
        var img_width = $(item).width();
        var div_width = $(item).parent().width();
        var newMargin = (div_width-img_width)/2+'px';
        $(item).css({'margin-left': newMargin});
    }
});

The JQuery way - CROP TO FIT (NO WHITESPACE):

$('img.shelf-img').each(function(i, item) {
    var img_height = $(item).height();
    var div_height = $(item).parent().height();
    if(img_height<div_height){
        //INCREASE HEIGHT OF IMAGE TO MATCH CONTAINER
        $(item).css({'width': 'auto', 'height': div_height });
        //GET THE NEW WIDTH AFTER RESIZE
        var img_width = $(item).width();
        //GET THE PARENT WIDTH
        var div_width = $(item).parent().width();
        //GET THE NEW HORIZONTAL MARGIN
        var newMargin = (div_width-img_width)/2+'px';
        //SET THE NEW HORIZONTAL MARGIN (EXCESS IMAGE WIDTH IS CROPPED)
        $(item).css({'margin-left': newMargin });
    }else{
        //CENTER IT VERTICALLY (EXCESS IMAGE HEIGHT IS CROPPED)
        var newMargin = (div_height-img_height)/2+'px';
        $(item).css({'margin-top': newMargin});
    }
});
于 2013-10-14T13:34:01.690 回答
1

If you want to keep the image ratio, I would set max-height and max-width to 100%. Here's a sample to show how that works. That will effectively shrink images that are larger than the div, but it will keep the aspect ratio.

For images that are smaller than the div, you will have to scale up with JavaScript. The basic algorithm is like so:

  1. Find the aspect ratio of the image (width / height)
  2. Find the aspect ratio of the div (width / height)
  3. If the image's aspect ratio is less than the div's, set the image's height to 100%
  4. If the image's aspect ratio is greater than the div's, set the image's width to 100%
  5. Whichever dimension is not set, set it to auto

Obviously, you could use this algorithm for scaling up or down, but if you can be guaranteed that your div will always be smaller than your image, you can use the simpler CSS solution.

It looks like you've got code to do centering, so I'll leave that to you.

于 2013-10-14T15:05:26.260 回答