在保持纵横比的同时调整子图像大小的更好方法

时间:2018-02-12 14:46:28

标签: html css

我想知道是否有更好的方法来维持图像的图像宽高比(例如翻转拇指)。它目前使用3 * 2大的透明图像;没有它们,你看不到图像。我尝试使用的是3 * 2的div,但却没有用。

有没有其他方法可以做到这一点?因为每次加载透明图像时,都会给服务器带来压力,因为图像必须链接到某些东西。我也尝试将它改为没有来源的图像,即:

<img src="" width=3 height=2>

而不是:

<img src="https://cml.sad.ukrd.com/tp/3x2/">

但它没有用。如果他们没有链接到服务器,我不介意使用图像。很想听听你的想法!

&#13;
&#13;
#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;
}
#test {
  width: 100%;
  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;
  padding-bottom: 66.6666666666666%;
}
canvas {
  display: block;
  width: 100%;
  background-position: center;
  background-size: cover;
  margin-bottom: 6px;
}

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

.image-thumb>img {
  height: auto;
  width: 100%;
}
&#13;
<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="data:image/svg+xml;charset=utf-8,%3Csvg xmlns%3D'http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg' viewBox%3D'0 0 3 2'%2F%3E" width="3" height="2" 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')">
      
    </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')">
      
    </div>
    <canvas 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')" width=3 height=2>
    </canvas>
    <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')">
      
    </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')">
      
    </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')">
      
    </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>
&#13;
&#13;
&#13;

注意:调整浏览器窗口大小仍会保持图像宽高比

更新:似乎画布标签几乎可以解决问题,但留下的边距看起来似乎无法调整!

更新2:似乎canvas标签可能已经解决了它

更新3:代码现在包括画布和填充解决方案

1 个答案:

答案 0 :(得分:1)

您可以在padding-bottom上使用image-thumb并将其设置为%的图像比例。

我从587/800得到73.375%,对于第一张老虎图像,我删除了所有透明占位符。如果您只想要2/3

,请使用66.66666%

&#13;
&#13;
#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;
}
#test {
  width: 100%;
  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;
  padding-bottom: 73.375%;
}
canvas {
  display: block;
  width: 100%;
  background-position: center;
  background-size: cover;
  margin-bottom: 6px;
}

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

.image-thumb>img {
  height: auto;
  width: 100%;
}
&#13;
<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="data:image/svg+xml;charset=utf-8,%3Csvg xmlns%3D'http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg' viewBox%3D'0 0 3 2'%2F%3E" width="3" height="2" 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')">
      
    </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')">
      
    </div>
    <canvas 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')" width=3 height=2>
    </canvas>
    <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')">
      
    </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')">
      
    </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')">
      
    </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>
&#13;
&#13;
&#13;