【问题标题】:Common height of last three images only仅最后三个图像的共同高度
【发布时间】:2017-05-12 13:25:41
【问题描述】:

我在一个页面上有 20 多张图片,我正在尝试将最后三张图片的高度调整为相等,但我不能,谁能帮助我如何使用 jquery 来做到这一点 strong> 或 css 之类的(最后三个孩子条件

html 是:

<div class="col-md-4">
    <img class="thumbs">
</div>

css 是:

img {
width:100%;
height: auto;
}

我想在 ref image 中等于上面显示的最后三张图片

【问题讨论】:

标签: jquery css frontend


【解决方案1】:

$(function() {
  function getLastOf(selector, count) {
    var allSpans = $(selector);
    if(count > allSpans.length) {
      return allSpans;
    }
    return allSpans.slice(allSpans.length-count);
  }
  var spans = getLastOf('span',3);
  spans.css('background-color','red');
  var highest = 0;
  spans.each(function(i,e) {
      highest = $(e).height() > highest ? $(e).height() : highest;
  });
  console.log("The highest span is " + highest + "px.");

  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <span>Text1</span>
    <span>Text2</span>
    <span>Text3</span>
    <span>Text4</span>

这应该可以轻松更改为您的图像。

【讨论】:

  • 但是由于图像的高度不相等,我怎么知道最后三张图像所需的高度是多少?
  • 我理解你的方式是你想自己计算高度。老实说,我以为您的意思是平均水平。我不知道你想要什么。但是有了这三个,你可以做getLastOf('div &gt; img', 3).each(function(index,element) { /* work with what you need */});之类的事情。例如,您可以取这三个中的平均值或最高值。
  • 简而言之,我想让最后三个图像大小 100% 到页脚高度
  • 好吧,如果你想用 jQuery 做到这一点,你只需要在每个函数中添加它。您可以获取页脚的高度,然后添加$(element).height(yourFooterheight)。但是,如果您希望它是动态的,那么 css 方法可能会更好。您是否考虑过将这些图像放在具有 position: relative 和固定高度的元素中,然后将它们设置为高度 100%?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-12
  • 2018-01-30
相关资源
最近更新 更多