关于图片宽高的等比缩放,其实需求就是让图片自适应父容器的宽高,并且是等比缩放图片,使图片不变形。

例如,需要实现如下的效果:

要实现上面的效果,需要知道图片的宽高,父容器的宽高,然后计算缩放后的宽高。

首先,图片的宽高和父容器的宽高都能方便的获取到,然后,等比缩放的算法如下:

 function scalingImage(imgWidth, imgHeight, containerWidth, containerHeight) {
var containerRatio = containerWidth / containerHeight;
var imgRatio = imgWidth / imgHeight; if (imgRatio > containerRatio) {
imgWidth = containerWidth;
imgHeight = containerWidth / imgRatio;
} else if (imgRatio < containerRatio) {
imgHeight = containerHeight;
imgWidth = containerHeight * imgRatio;
} else {
imgWidth = containerWidth;
imgHeight = containerHeight;
} return { width: imgWidth, height: imgHeight };
}

接着,如何让图片居中显示呢?

方法一:可以设置img标签的margin-left和margin-top属性实现,这个可用通过父容器的宽高和图片缩放后的宽高计算出来。如下:

    var marginLeft = (containerWidth - width) / ;
var marginTop = (containerHeight - height) / ;

方法二:通过设置父容器的height和line-height属性值相同,以及img标签属性 vertical-align: middle; ,让img标签垂直居中;设置父容器属性 text-align: center; ,让img标签水平居中。

如何让图片加载完成就自适应宽高呢?

这个可以绑定img标签的onload事件,代码如下:

    <div class="img-container">
<img src="data:images/1.png" onload="scalingImg(this)" width="" height="">
</div>
 function scalingImg(obj){
var $this = $(obj); var imgWidth=$this.width();
var imgHeight=$this.height(); var parent = $this.parent();
var containerWidth = parent.width();
var containerHeight=parent.height(); var containerRatio = containerWidth / containerHeight;
var imgRatio = imgWidth / imgHeight; if (imgRatio > containerRatio) {
imgWidth = containerWidth;
imgHeight = containerWidth / imgRatio;
} else if (imgRatio < containerRatio) {
imgHeight = containerHeight;
imgWidth = containerHeight * imgRatio;
} else {
imgWidth = containerWidth;
imgHeight = containerHeight;
} $this.attr('width',imgWidth);
$this.attr('height',imgHeight);
}

最后,附上demo源码如下:ScaleImageDemo.zip

JS实现图片宽高的等比缩放的更多相关文章

  1. js 获取图片宽高 和 图片大小

    获取要查看大小的img var img_url = 'http://img5.imgtn.bdimg.com/it/u=4267222417,1017407570&fm=200&gp= ...

  2. js不需要知道图片宽高的懒加载方法(经过实际测试,不加宽高仍然是无法正常加载的,设置height:auto,height:100%,仍然显示高度为0)

    js不需要知道图片宽高的懒加载方法 懒加载是如何实现的? - 简书https://www.jianshu.com/p/e86c61468285找到一个不需要知道图片宽高的懒加载方法了(经过实际测试,不 ...

  3. 写个js动态调整图片宽高 (原创)

    <body style="TEXT-ALIGN: center;"> <div id="testID" style="backgro ...

  4. JS快速获取图片宽高的方法

    快速获取图片的宽高其实是为了预先做好排版样式布局做准备,通过快速获取图片宽高的方法比onload方法要节省很多时间,甚至一分钟以上都有可能,并且这种方法适用主流浏览器包括IE低版本浏览器. 我们一步一 ...

  5. 转载:JS快速获取图片宽高的方法

    快速获取图片的宽高其实是为了预先做好排版样式布局做准备,通过快速获取图片宽高的方法比onload方法要节省很多时间,甚至一分钟以上都有可能,并且这种方法适用主流浏览器包括IE低版本浏览器. 我们一步一 ...

  6. php 图片上传的公共方法(按图片宽高缩放或原图)

    写的用于图片上传的公共方法类调用方法: $upload_name='pic';$type = 'logo_val';$file_name = 'logo_' . $user_id .create_st ...

  7. css3圆形头像(当图片宽高不相等时)

    1.图片宽高相等,width:300px: height:300px; 把他变成宽高100px的圆形头像 img{width:100px; height:100px; border-radius:50 ...

  8. js实现未知宽高的元素在指定元素中垂直水平居中

    js实现未知宽高的元素在指定元素中垂直水平居中:本章节介绍一下如何实现未知宽高的元素在指定元素下实现垂直水平居中效果,下面就以span元素为例子,介绍一下如何实现span元素在div中实现水平垂直居中 ...

  9. css+background实现 图片宽高自适应,拉伸裁剪不变形

    图片宽高不固定 ,一样实现自适应,拉伸裁剪不变形,适应各大兼容性.  下面咱们在网上找两张宽高不一样的照片:     No.1                                      ...

随机推荐

  1. [转贴]C语言复习笔记-17种小算法-解决实际问题

    判断日期为一年中的第几天(考虑闰年) /* * 计算该日在本年中是第几天,注意闰年问题 * 以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天 * 特殊情况,闰年且输入月份大于3时 ...

  2. SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-005-Bean的作用域@Scope、ProxyMode

    一. Spring的bean默认是单例的 But sometimes you may find yourself working with a mutable class that does main ...

  3. Android ListView异步加载数据

    1.主Activity public class MainActivity extends Activity { private ListView listView; private ArrayLis ...

  4. LintCode 38. Search a 2D Matrix II

    Write an efficient algorithm that searches for a value in an m x n matrix, return the occurrence of ...

  5. bzoj3091

    最近屯题都忘了把解题报告写上了这道题是一道比较烦的LCT,我们先考虑每个点上到底要维护什么我们设路径上有n个点,从起点到终点编号为1~n显然期望=S/[(n+1)n div 2]S=∑a[i]*i*( ...

  6. 转载--C语言运算符优先级和口诀

    转载:http://www.cnblogs.com/zhanglong0426/archive/2010/10/06/1844700.html 一共有十五个优先级: 1   ()  []  .  -& ...

  7. datatables配置及数据传输

    var merchant_url = "index.php?op=merchant"; var table_merchant_setting ={ "ajax" ...

  8. 好用的ajax后台框架

    dwz 简单实用的国产jquery Ui框架 http://www.j-ui.com/#_blank

  9. MVC Model 数据注解与验证

    常用验证特性: using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Sch ...

  10. Bzoj 1532: [POI2005]Kos-Dicing 二分,网络流

    1532: [POI2005]Kos-Dicing Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1373  Solved: 444[Submit][St ...