简体   繁体   English

使用flexbox分发图像,以便使用jquery图像高度相等-Firefox与Chrome行为

[英]Distributing images with flexbox so images are equal height using jquery - Firefox vs Chrome behaviour

I'm using jquery to calculate the aspect ratio of images and setting that as the flex-grow value so that all images within the flexbox appear the same height while filling the width of the container. 我正在使用jquery来计算图像的长宽比,并将其设置为flex-grow值,以使flexbox内的所有图像在填充容器的宽度时都显示相同的高度。

This works great in Firefox, but has different behaviour in Chrome. 这在Firefox中效果很好,但在Chrome中却有不同的行为。

In Firefox, all images are the same height and maintain their aspect ratios. 在Firefox中,所有图像的高度均相同,并保持其纵横比。

In Chrome, all images appear to have the same width as in Firefox, but they are vertically stretched to the height of the tallest item, 500px. 在Chrome浏览器中,所有图像的宽度看上去都与Firefox中的宽度相同,但它们在垂直方向上拉伸到了最高像素500px的高度。

How can I fix how this looks in Chrome? 如何修复Chrome中的外观?

 $('div.pack').css({ "display": "flex" }) .children().each(function() { var aspect = this.naturalWidth / this.naturalHeight; $(this).wrap('<div style="display:flex; flex: 0% ' + aspect + ' 1;" />') }); 
 img { width: 100%; height: auto; } div.pack > div:not(:last-child) { margin-right: 2%; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class='pack'> <img src="http://via.placeholder.com/400x150"/> <img src="http://via.placeholder.com/150x500"/> <img src="http://via.placeholder.com/300x300"/> </div> 

You will need to add align-items: flex-start to the div wrapper's CSS, or else Chrome will stretch the img as the default value for align-items is just that, stretch . 您将需要添加align-items: flex-startdiv包装器的CSS,否则Chrome会拉伸 img ,因为align-items的默认值就是该stretch

I also changed the order of the shorthand flex properties to their correct position 我还将速记flex属性的顺序更改为正确的位置

Also note, using percent for margin and padding on flex items might render different based on the fact that the specs. 另请注意,基于规格的事实, 对柔性项目使用边距和填充百分比可能会有所不同。 allows it, and it is recommended not to. 允许它,建议不要这样做。

Stack snippet 堆栈片段

 $('div.pack').css({ "display": "flex" }) .children().each(function() { var aspect = this.naturalWidth / this.naturalHeight; $(this).wrap('<div style="display: flex; align-items: flex-start; flex: ' + aspect + ' 1 0%;" />') }); 
 img { width: 100%; height: auto; } div.pack > div:not(:last-child) { margin-right: 2%; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class='pack'> <img src="http://via.placeholder.com/400x150"/> <img src="http://via.placeholder.com/150x500"/> <img src="http://via.placeholder.com/300x300"/> </div> 


Another option would be to simply remove display: flex on the div wrapper's. 另一个选择是简单地删除display: flex div包装器上的display: flex

Stack snippet 堆栈片段

 $('div.pack').css({ "display": "flex" }) .children().each(function() { var aspect = this.naturalWidth / this.naturalHeight; $(this).wrap('<div style="flex: ' + aspect + ' 1 0%;" />') }); 
 img { width: 100%; height: auto; } div.pack > div:not(:last-child) { margin-right: 2%; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class='pack'> <img src="http://via.placeholder.com/400x150"/> <img src="http://via.placeholder.com/150x500"/> <img src="http://via.placeholder.com/300x300"/> </div> 

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

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