console
/**
* 已知图片的宽度和高度的等比例缩放
*/
function knowImgSize(id) {
var idWidth = $(id).width(), // 容器的宽度和高度
idHeight = $(id).height();
$(id + ' img').each(function(index,img){
var img_w = $(this).width(),
img_h = $(this).height();
// 如果图片自身宽度大于容器的宽度的话 那么高度等比例缩放
if(img_w > idWidth) {
var height = img_h * idWidth / img_w;
$(this).css({"width":idWidth, "height":height});
}
});
}
// 初始化
$(function(){
knowImgSize("#demo1");
});
<img src="http://pic.haoanyi.cn/pic/20160928/m_d6e9a091cf89383172905bb0e532f38d.jpg" width="1060" height="300" alt="">
<div id="demo1">
<img src="http://pic.haoanyi.cn/pic/20160928/m_d6e9a091cf89383172905bb0e532f38d.jpg" alt="">
</div>
#demo1{width:800px;height:300px;overflow:hidden;}