JS实现图片宽高的等比缩放
关于图片宽高的等比缩放,其实需求就是让图片自适应父容器的宽高,并且是等比缩放图片,使图片不变形。
例如,需要实现如下的效果:

要实现上面的效果,需要知道图片的宽高,父容器的宽高,然后计算缩放后的宽高。
首先,图片的宽高和父容器的宽高都能方便的获取到,然后,等比缩放的算法如下:
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实现图片宽高的等比缩放的更多相关文章
- js 获取图片宽高 和 图片大小
获取要查看大小的img var img_url = 'http://img5.imgtn.bdimg.com/it/u=4267222417,1017407570&fm=200&gp= ...
- js不需要知道图片宽高的懒加载方法(经过实际测试,不加宽高仍然是无法正常加载的,设置height:auto,height:100%,仍然显示高度为0)
js不需要知道图片宽高的懒加载方法 懒加载是如何实现的? - 简书https://www.jianshu.com/p/e86c61468285找到一个不需要知道图片宽高的懒加载方法了(经过实际测试,不 ...
- 写个js动态调整图片宽高 (原创)
<body style="TEXT-ALIGN: center;"> <div id="testID" style="backgro ...
- JS快速获取图片宽高的方法
快速获取图片的宽高其实是为了预先做好排版样式布局做准备,通过快速获取图片宽高的方法比onload方法要节省很多时间,甚至一分钟以上都有可能,并且这种方法适用主流浏览器包括IE低版本浏览器. 我们一步一 ...
- 转载:JS快速获取图片宽高的方法
快速获取图片的宽高其实是为了预先做好排版样式布局做准备,通过快速获取图片宽高的方法比onload方法要节省很多时间,甚至一分钟以上都有可能,并且这种方法适用主流浏览器包括IE低版本浏览器. 我们一步一 ...
- php 图片上传的公共方法(按图片宽高缩放或原图)
写的用于图片上传的公共方法类调用方法: $upload_name='pic';$type = 'logo_val';$file_name = 'logo_' . $user_id .create_st ...
- css3圆形头像(当图片宽高不相等时)
1.图片宽高相等,width:300px: height:300px; 把他变成宽高100px的圆形头像 img{width:100px; height:100px; border-radius:50 ...
- js实现未知宽高的元素在指定元素中垂直水平居中
js实现未知宽高的元素在指定元素中垂直水平居中:本章节介绍一下如何实现未知宽高的元素在指定元素下实现垂直水平居中效果,下面就以span元素为例子,介绍一下如何实现span元素在div中实现水平垂直居中 ...
- css+background实现 图片宽高自适应,拉伸裁剪不变形
图片宽高不固定 ,一样实现自适应,拉伸裁剪不变形,适应各大兼容性. 下面咱们在网上找两张宽高不一样的照片: No.1 ...
随机推荐
- 编译GNU/Linux共享库, 为什么要用PIC编译?
http://blog.csdn.net/chenji001/article/details/5691690
- SPRING IN ACTION 第4版笔记-第二章WIRING BEANS-007-以set方法注入<property>\p-namespace\util-space
一.注入简单属性 package soundsystem.properties; import org.springframework.beans.factory.annotation.Autowir ...
- NAND Flash的基本操作——读、写、擦除
基本操作 这里将会简要介绍一下NAND Flash的基本操作在NAND Flash内部是如何进行的,基本操作包括:读.写和擦除. 读: 当我们读取一个存储单元中的数据时(如图2.4),是使 ...
- 最简单的CRC32源码-逐BYTE法
从按BIT计算转到按BYTE计算,要利用异或的一个性质,具体见前面的文章<再探CRC >. 其实方法跟逐BIT法是一样的,我们只是利用异或的性质,把数据分成一BYTE一BYTE来计算,一B ...
- 查看本机上的端口使用情况netstat -an
1.查找本机上的端口使用情况 netstat -an 2.查找指定端口的使用情况 C:\Windows\System32>netstat -ano | find "8002" ...
- op论坛,分支
http://www.arm9home.net/thread.php?fid=68 http://www.openwrt.org.cn/bbs/forum.php https://dev.openwr ...
- Red and Black
Red and Black Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- 豆约翰博客备份专家博客导出示例(PDF,CHM)
示例1: CSDN博客: 示例博客主页:http://blog.csdn.net/shenyisyn/ CHM文件图示:下载地址 PDF文件图示:下载地址 示例2: 新浪博客: 示例博客主页:http ...
- ARM学习笔记10——GNU ARM命令行工具
一.编译器arm-linux-gcc 1.用arm-linux-gcc编译一个程序,一般它是要经过如下步骤的: 1.1.预处理阶段 编译器把上述代码中stdio.h编译进来,使用GCC的选项-E可以使 ...
- APMServ5.2.6 升级PHP版本 到高版本 5.3,5.4
首先下载:http://windows.php.net/downloads/releases/php-5.3.28-Win32-VC9-x86.zip Thursday, December 12, ...