【问题标题】:Equal height image grid that fills up width of container填充容器宽度的等高图像网格
【发布时间】:2017-06-21 02:55:46
【问题描述】:

这是我想要实现的目标:

  • 1 行,里面有 3 张图片。
  • 1 行,里面有 2 张图片。

如何使 100%-width-container 中的图像具有相同的高度?

所有图片都没有一致的宽度和高度。 我需要图像来保持它们的裁剪和纵横比。 它们不需要具有相同的宽度。

到目前为止,这是我使用 flex-box 取得的成果。但它们的高度不同。我不介意使用 JavaScript 来解决这个问题。

我宁愿不为容器或图像设置高度值。 有没有办法让图像在具有相同高度的同时均匀分布?

.wrapper {
  width:100vw; /*should be width of browser*/
  display:flex;
  margin:0;
  padding:0;
  margin:0;
  border:1px solid;
}

.image {
  list-style:none;
  margin: 50px 1.5%;
}
.image:first-child {
  margin-left:0;
}
.image:last-child {
  margin-right:0;
}
img {
  width:100%
}
<ul class="wrapper three_images">
  <li class="image"><img src="http://lorempixel.com/600/800/"></li>
  <li class="image"><img src="http://lorempixel.com/400/850/"></li>
  <li class="image"><img src="http://lorempixel.com/550/700/"></li>
</ul>



<ul class="wrapper two_images">
  <li class="image"><img src="http://lorempixel.com/600/800/"></li>
  <li class="image"><img src="http://lorempixel.com/400/850/"></li>
</ul>

【问题讨论】:

  • 基本上,您不能在不扭曲图像的情况下对 inline 图像执行此操作。
  • 有什么替代内嵌图片的方法?
  • 如果图片的高度不同,应该缩放哪个图片?

标签: javascript html css flexbox


【解决方案1】:

这是一个使用 jQuery 的解决方案:

$(".wrapper").each(function () {
  /** Push all images size in an array **/
  let imagesSize = [];
  $(this)
    .find("img")
    .each(function () {
      let imgWidth = $(this).width();
      let imgHeight = $(this).height();
      imagesSize.push([imgWidth, imgHeight]);
    });

  /** Assign the same height to all the images **/
  let newImagesSize = [];
  let totalWidth = 0;
  let referenceHeight = imagesSize[0][1];

  imagesSize.forEach(function (imageSize, index) {
    let heightRatio = referenceHeight / imageSize[1];
    let newImgWidth = imageSize[0] * heightRatio;
    let newImgHeight = imageSize[1] * heightRatio;
    newImagesSize.push([newImgWidth, newImgHeight]);
    totalWidth += newImgWidth;
  });

  /** Get the total width of all the images, the width of the wrapper and addapt the width/height of each images **/
  let wrapperWidth = $(this).width();
  let widthRatio = wrapperWidth / totalWidth;

  $(this)
    .find("img")
    .each(function (index, el) {
      let imgWidth = newImagesSize[index][0] * widthRatio;
      let imgHeight = newImagesSize[index][1] * widthRatio;
      $(this).css({ width: imgWidth, height: imgHeight });
    });
});

$(document).ready(function () {
  $(".wrapper").each(function (index, el) {
    /** Push all images size in an array **/
    let imagesSize = [];
    $(this)
      .find("img")
      .each(function () {
        let imgWidth = $(this).width();
        let imgHeight = $(this).height();
        imagesSize.push([imgWidth, imgHeight]);
      });

    /** Assign the same height to all the images **/
    let newImagesSize = [];
    let totalWidth = 0;
    let referenceHeight = imagesSize[0][1];

    imagesSize.forEach(function (imageSize, index) {
      let heightRatio = referenceHeight / imageSize[1];
      let newImgWidth = imageSize[0] * heightRatio;
      let newImgHeight = imageSize[1] * heightRatio;
      newImagesSize.push([newImgWidth, newImgHeight]);
      totalWidth += newImgWidth;
    });

    /** Get the total width of all the images, the width of the wrapper and addapt the width/height of each images **/
    let wrapperWidth = $(this).width();
    let widthRatio = wrapperWidth / totalWidth;

    $(this)
      .find("img")
      .each(function (index, el) {
        let imgWidth = newImagesSize[index][0] * widthRatio;
        let imgHeight = newImagesSize[index][1] * widthRatio;
        $(this).css({ width: imgWidth, height: imgHeight });
      });
  });
});
.image {
  float: left;
}

li {
  list-style-type:none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<ul class="wrapper three_images">
  <li class="image"><img src="https://via.placeholder.com/300x750.jpeg/FFFF00"></li>
  <li class="image"><img src="https://via.placeholder.com/400x300.jpeg/0000FF"></li>
  <li class="image"><img src="https://via.placeholder.com/500x550.jpeg/FF0000"></li>
</ul>



<ul class="wrapper two_images">
  <li class="image"><img src="https://via.placeholder.com/600x800.jpeg/808080"></li>
  <li class="image"><img src="https://via.placeholder.com/400x850.jpeg/dddddd"></li>
</ul>

【讨论】:

【解决方案2】:

您可以在两个轴上使用 object-fit 和尺寸 img ,它会自行裁剪:

您也可以使用 object-positionimage-orientationimage-rendering 进行调整

img {
  width:100%;
  height:100%;
  object-fit: cover;
}

.wrapper {
  width:100vw; /*should be width of browser*/
  display:flex;
  margin:0;
  padding:0;
  margin:0;
  border:1px solid;
}
body {margin:0;}

.image {
  list-style:none;
  margin: 50px 1.5%;
}
.image:first-child {
  margin-left:0;
}
.image:last-child {
  margin-right:0;
}
img {
  width:100%;
  height:100%;
  object-fit: cover;
}
<ul class="wrapper three_images">
  <li class="image"><img src="http://lorempixel.com/600/800/"></li>
  <li class="image"><img src="http://lorempixel.com/400/850/"></li>
  <li class="image"><img src="http://lorempixel.com/550/700/"></li>
</ul>



<ul class="wrapper two_images">
  <li class="image"><img src="http://lorempixel.com/600/800/"></li>
  <li class="image"><img src="http://lorempixel.com/400/850/"></li>
</ul>

见:https://caniuse.com/object-fit

它的工作方式基本上就像在背景中设置图像并通过背景大小,背景位置调整大小,但只有相似,行为和语义不同:(

【讨论】:

  • 只是想检查一下,当我使用适合对象的封面时,图像是否有假裁剪?
  • @wsgg 是的,图像在需要的地方被裁剪,否则比例会丢失,对象位置可以帮助调整应该裁剪哪一侧
【解决方案3】:
.wrapper {
  width:100vw; /*should be width of browser*/
  display:flex;
  margin:0;
  padding:0;
  margin:0;
  border:1px solid;
}

.image {
  list-style:none;
  margin: 50px 1.5%;
  height: 100px; /* SET THIS */
  overflow: hidden;
}
.image:first-child {
  margin-left:0;
}
.image:last-child {
  margin-right:0;
}
img {
  width:100%
  height: 100%;
}

试试这个。只需将 .image 高度值设置为您觉得合适的任何值。

【讨论】:

  • 我忘了说不要为容器或图像设置高度值。有没有办法让图像在具有相同高度的同时均匀分布?
  • 啊不,我在问题中提到“我需要图像来保持它们的裁剪和纵横比”。嗯谢谢你的帮助
猜你喜欢
  • 2022-01-12
  • 1970-01-01
  • 1970-01-01
  • 2017-02-25
  • 1970-01-01
  • 1970-01-01
  • 2020-02-03
  • 2022-01-09
相关资源
最近更新 更多