使父母的身高等于其中一个孩子

时间:2018-02-09 10:06:29

标签: javascript html css

我试图让粉红色的#images-wrap'与主图像具有相同的height。每当右侧有太多小翻转图像时,它会推动粉红色height的{​​{1}}超过主图像。如果我可以让它匹配它的高度,那么我可以使用div不显示下面的多余翻转图像并使用overflow作为scroll-y以便可以向下滚动以查看多余的小图像

以表格形式显示并不起作用 - 您认为一个简单的孩子overflow hidden和隐藏的溢出就可以解决这个问题,但是您无法做到这一点。设置高度,否则图像宽高比不会调整大小。图片必须保留div

此解决方案中的javascript并不起作用,因为它可能无法获取图像的高度。我也试图获取子图像的高度,这也失败了。

有没有人知道可以实现这一目标的魔术?

非常感谢这里的任何帮助,谢谢!



3:2 aspect ratios

#images-wrap {
  width: 50%;
  height: auto;
  margin-top: 25px;
  float: left;
  display: flex;
  background: red;
  max-height: 150px;
}

#details-wrap {
  width: 100%;
  height: 325px;
  float: left;
  text-align: right;
  position: relative;
}

#main-image {
  width: 80.5%;
  float: left;
  background-size: cover !important;
  background-position: center center !important;
  height: auto;
}

#main-image>img {
  display: block;
  width: 100%;
  height: auto;
  margin: 0;
}

#image-thumbs {
  width: 17.5%;
  height: auto;
  float: left;
  margin-left: 2%;
  overflow-y: scroll !important;
  /* make it only scroll when exceeds height of main image */
  /* max-height: 400px;  make this the height of #main-image */
}

.image-thumb {
  margin-bottom: 6px;
  background-position: center;
  background-size: cover;
  height: auto;
}

.image-thumb:last-of-type {
  margin-bottom: 0;
}

.image-thumb>img {
  height: auto;
  width: 100%;
}




1 个答案:

答案 0 :(得分:2)

您可以在主容器上使用display: flex

#images-wrap {
  width: 100%;
  height: auto;
  margin-top: 25px;
  float: left;
  display: flex;
}

#details-wrap {
  width: 100%;
  height: 325px;
  float: left;
  text-align: right;
  position: relative;
}

#main-image {
  width: 80.5%;
  float: left;
  background-size: cover !important;
  background-position: center center !important;
  height: auto;
}

#main-image>img {
  display: block;
  width: 100%;
  height: auto;
  margin: 0;
}

#image-thumbs {
  width: 17.5%;
  height: auto;
  float: left;
  margin-left: 2%;
  overflow-y: scroll !important;
  /* make it only scroll when exceeds height of main image */
  /* max-height: 400px;  make this the height of #main-image */
}

.image-thumb {
  margin-bottom: 6px;
  background-position: center;
  background-size: cover;
  height: auto;
}

.image-thumb:last-of-type {
  margin-bottom: 0;
}

.image-thumb>img {
  height: auto;
  width: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div id="images-wrap">
  <div id="main-image" style="background-image: url('http://elephant-family.org/wp-content/uploads/2015/06/shutterstock_77217466.jpg')">
    <img src="https://cml.sad.ukrd.com/tp/3x2/" id="main-image-sizer">
  </div>
  <div id="image-thumbs">
    <div class="image-thumb" onmouseover="$('#main-image').css('background-image', $(this).css('background-image'));" style="background-image: url('https://imagesvc.timeincapp.com/v3/mm/image?url=https%3A%2F%2Ffortunedotcom.files.wordpress.com%2F2014%2F09%2F174187214.jpg&w=800&q=85')">
      <img src="https://cml.sad.ukrd.com/tp/3x2/">
    </div>
    <div class="image-thumb" onmouseover="$('#main-image').css('background-image', $(this).css('background-image'));" style="background-image: url('http://streamafrica.com/wp-content/uploads/2014/01/african-lion-wallpapers-hd-648x372.jpg')">
      <img src="https://cml.sad.ukrd.com/tp/3x2/">
    </div>
    <div class="image-thumb" onmouseover="$('#main-image').css('background-image', $(this).css('background-image'));" style="background-image: url('https://imagesvc.timeincapp.com/v3/mm/image?url=https%3A%2F%2Ffortunedotcom.files.wordpress.com%2F2014%2F09%2F174187214.jpg&w=800&q=85')">
      <img src="https://cml.sad.ukrd.com/tp/3x2/">
    </div>
    <div class="image-thumb" onmouseover="$('#main-image').css('background-image', $(this).css('background-image'));" style="background-image: url('http://streamafrica.com/wp-content/uploads/2014/01/african-lion-wallpapers-hd-648x372.jpg')">
      <img src="https://cml.sad.ukrd.com/tp/3x2/">
    </div>
    <div class="image-thumb" onmouseover="$('#main-image').css('background-image', $(this).css('background-image'));" style="background-image: url('https://imagesvc.timeincapp.com/v3/mm/image?url=https%3A%2F%2Ffortunedotcom.files.wordpress.com%2F2014%2F09%2F174187214.jpg&w=800&q=85')">
      <img src="https://cml.sad.ukrd.com/tp/3x2/">
    </div>
    <div class="image-thumb" onmouseover="$('#main-image').css('background-image', $(this).css('background-image'));" style="background-image: url('http://streamafrica.com/wp-content/uploads/2014/01/african-lion-wallpapers-hd-648x372.jpg')">
      <img src="https://cml.sad.ukrd.com/tp/3x2/">
    </div>
    <script>
      // hides overflow scroll if less than 5 thumbs
      var thumbs = document.getElementsByClassName('image-thumb');
      var thumbsWrap = document.getElementById('image-thumbs');
      if (thumbs.length < 5) {
        thumbsWrap.style.overflow = 'hidden';
      }
    </script>
    <script>
      // makes '#image-thumbs' not exceed the height of '#main-image'
      var mainImgHeight = document.getElementById('main-image-sizer').style.height;
      var imageThumbsInitialHeight = document.getElementById('image-thumbs').style.height;
      if (imageThumbsInitialHeight > mainImgHeight) {
        document.getElementById('image-thumbs').style.height = mainImgHeight;
      }
    </script>
  </div>
</div>