简体   繁体   English

使图像适合容器,即使更改了src

[英]Fit image into a container, even when src is changed

I'm sure there are other post that address this in various ways. 我敢肯定还有其他帖子以各种方式解决了这个问题。

I've been struggling with this a bit, trying to do CSS only approach with no luck. 我一直在为此苦苦挣扎,尝试做纯CSS的方法没有运气。 Playing around with css width and height, doing a mix of 100% and auto on the image got me nowhere. 玩css的宽度和高度,对图像进行100%自动混合使我无处可去。

Given html: 给定html:

<div class="container">
    <img src="http://image" />
</div>

And css: 和CSS:

.container { width: 500px; height: 400px; }

How to fit images of various sizes into the container while preserving aspect ratio and obeying constraints of the container? 如何在保持宽高比和遵循容器约束的同时将各种尺寸的图像放入容器中?

I believe you are far over-thinking this. 我相信您对此太过思索了。

Try adding the following CSS, and removing the javascript: 尝试添加以下CSS,并删除javascript:

.container img {
    max-width:  100%;
    max-height: 100%;
}

Here's a demo (click the images to dynamically load a different size) 这是一个演示 (单击图像以动态加载其他大小)

I ended up with javascript solution - I know, not ideal but it's the best I could come up with that does what I need it to. 我最终得到了javascript解决方案-我知道,这并不理想,但这是我能想到的最好的解决方案。 The page where I'm using this relies on javascript to perform other bits of functionality so that's not a problem in my scenario. 我正在使用此页面的页面依靠javascript执行其他功能,因此在我的方案中这不是问题。 I'm also loading images into a Bootstrap grid layout column, like so: 我还将图像加载到Bootstrap网格布局列中,如下所示:

<div class="row">
    <div class="col-md-6">
        <div class="main-image">
            <img src="http://initialimage" />
        </div>
    </div>
    <div class="col-md-6">
        Some content here ...
    </div>
</div>

The width of the container in this case is controlled by bootstrap layout css so I only need to set the height of the container: 在这种情况下,容器的宽度由引导程序布局CSS控制,因此我只需要设置容器的高度即可:

.main-image { height: 400px; }

I tried with using just 我尝试使用

.main-image { max-height: 400px; }

So that the container would adjust to horizontally stretched images but then jQuery gets the container height wrong. 这样容器可以调整为水平拉伸的图像,但是jQuery却使容器高度错误。

Anyway, here's the javascript I'm using: 无论如何,这是我正在使用的javascript:

var imageLoaded = function () {
    var container = $(this).parent();
    var maxWidth = container.width();
    var maxHeight = container.height();
    var width = $(this).width();
    var height = $(this).height();
    var containerRatio = maxWidth / maxHeight;        
    var ratio = width / height;
    if (ratio > containerRatio) {
        $(this).attr('width', '100%;');
        $(this).attr('height', 'auto');
    }
    else {
        $(this).attr('height', '100%;');
        $(this).attr('width', 'auto');
    }
};

$(document).ready(function () {
    $('.main-image img').bind('load', imageLoaded);
    imageLoaded.call($('.main-image img'));
});

You may wonder why the manual invocation of imageLoaded function - it's because in IE, the event doesn't fire for the initially loaded image. 您可能想知道为什么手动调用imageLoaded函数-这是因为在IE中,该事件不会针对最初加载的图像触发。 Invoking it manually corrects that. 手动调用它可以纠正该问题。

You can then change the image source: 然后,您可以更改图像源:

$('.main-image img').attr('src', 'http://otherimage');

And the image will adjust to fit the container by either using up full height for vertical images or full width for horizontal images. 并且图像将通过使用垂直图像的全高或水平图像的全宽来调整以适合容器。

Any better way to do this or comments welcome. 任何更好的方式做到这一点或发表评论表示欢迎。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM