在保持纵横比的同时调整子图像大小的更好方法 [英] A better way of resizing a child image while keeping its aspect ratio

查看:30
本文介绍了在保持纵横比的同时调整子图像大小的更好方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有更好的方法来保持图像的图像纵横比(例如翻转拇指).目前使用的是3*2大的透明图片;没有它们,您将无法看到图像.我尝试使用 3*2 的 div 但没有用.

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

代替:

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

#images-wrap {宽度:100%;高度:自动;边距顶部:25px;向左飘浮;显示:弹性;}#详细信息包装{宽度:100%;高度:325px;向左飘浮;文本对齐:右;位置:相对;}#主图像{宽度:80.5%;向左飘浮;背景尺寸:覆盖!重要;背景位置:中心中心!重要;高度:自动;}#测试 {宽度:100%;高度:自动;}#main-image>img {显示:块;宽度:100%;高度:自动;边距:0;}#image-thumbs {宽度:17.5%;高度:自动;向左飘浮;左边距:2%;溢出-y:滚动!重要;/* 使其仅在超过主图像高度时滚动 *//* 最大高度:400px;使其成为#main-image 的高度 */}.image-thumb {底边距:6px;背景位置:中心;背景尺寸:封面;填充底部:66.66666666666666%;}帆布 {显示:块;宽度:100%;背景位置:中心;背景尺寸:封面;底边距:6px;}.image-thumb:last-of-type {底边距:0;}.image-thumb>img {高度:自动;宽度: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="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 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 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')">

<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')" 宽度=3 高度=2></画布><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 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 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')">

<脚本>//如果少于 5 个拇指,则隐藏溢出滚动var thumbs = document.getElementsByClassName('image-thumb');var thumbsWrap = document.getElementById('image-thumbs');如果 (thumbs.length <5) {thumbsWrap.style.overflow = '隐藏';}<脚本>//使 '#image-thumbs' 不超过 '#main-image' 的高度var mainImgHeight = document.getElementById('main-image-sizer').style.height;var imageThumbsInitialHeight = document.getElementById('image-thumbs').style.height;如果(imageThumbsInitialHeight > mainImgHeight){document.getElementById('image-thumbs').style.height = mainImgHeight;}

注意:调整浏览器窗口大小仍然保持图像纵横比

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

UPDATE 2:看来canvas标签可能已经解决了

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

解决方案

可以在 image-thumb 上使用 padding-bottom 并设置为图片的比例%.

我从 587/800 中获得了 73.375%,对于第一个老虎图像,我已经删除了所有透明的占位符.如果您只想要 2/3,请使用 66.66666%

#images-wrap {宽度:100%;高度:自动;边距顶部:25px;向左飘浮;显示:弹性;}#详细信息包装{宽度:100%;高度:325px;向左飘浮;文本对齐:右;位置:相对;}#主图像{宽度:80.5%;向左飘浮;背景尺寸:覆盖!重要;背景位置:中心中心!重要;高度:自动;}#测试 {宽度:100%;高度:自动;}#main-image>img {显示:块;宽度:100%;高度:自动;边距:0;}#image-thumbs {宽度:17.5%;高度:自动;向左飘浮;左边距:2%;溢出-y:滚动!重要;/* 使其仅在超过主图像高度时滚动 *//* 最大高度:400px;使其成为#main-image 的高度 */}.image-thumb {底边距:6px;背景位置:中心;背景尺寸:封面;填充底部:73.375%;}帆布 {显示:块;宽度:100%;背景位置:中心;背景尺寸:封面;底边距:6px;}.image-thumb:last-of-type {底边距:0;}.image-thumb>img {高度:自动;宽度: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="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 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 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')">

<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')" 宽度=3 高度=2></画布><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 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 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')">

<脚本>//如果少于 5 个拇指,则隐藏溢出滚动var thumbs = document.getElementsByClassName('image-thumb');var thumbsWrap = document.getElementById('image-thumbs');如果 (thumbs.length <5) {thumbsWrap.style.overflow = '隐藏';}<脚本>//使 '#image-thumbs' 不超过 '#main-image' 的高度var mainImgHeight = document.getElementById('main-image-sizer').style.height;var imageThumbsInitialHeight = document.getElementById('image-thumbs').style.height;如果(imageThumbsInitialHeight > mainImgHeight){document.getElementById('image-thumbs').style.height = mainImgHeight;}

I'm wondering whether there's a better way of maintaining the image aspect ratio of the images (e.g. the rollover thumbs). It currently uses transparent images that are 3*2 big; without them, you cannot see the images. I tried to use a div that's 3*2 instead but didn't work.

Is there an alternative method of doing this? Because every time the transparent images are loaded, it put strain on the server as the images have to be linked to something. I also tried changing it to images without a source, i.e.:

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

Instead of:

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

But it didn't work. I wouldn't mind using images if they didn't link to a server. Curious to hear your thoughts!

#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%;
}

<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>

Note: resizing the browser window still keeps the image aspect ratios

UPDATE: it seems that the canvas tag nearly does the trick, but leaves margin that doesn't seem to be adjustable!

UPDATE 2: it seems that the canvas tag may have solved it

UPDATE 3: code now includes both canvas and padding solutions

解决方案

You can use padding-bottom on image-thumb and set it to the ratio of the image in %.

I got 73.375% from 587 / 800, for that first tiger image, I have removed all the transparent placeholders. Use 66.66666% if you just want 2/3

#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%;
}

<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>

这篇关于在保持纵横比的同时调整子图像大小的更好方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
前端开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆