垂直调整浏览器窗口大小时按比例调整图像大小?

mat*_*att 5 javascript css jquery html5 responsive-design

我有文章,display:table里面有图像包装器,display:table-cell可实现图像的垂直和水平对齐。图像设置为宽度和高度的80%,因此在调整浏览器窗口大小时,图像应按比例缩放。

<article class="layer">
   <div class="wrap">
      <img src="whatever.jpg" alt="image"/>
   </div>
</article>

html, body, #content {
    margin: 0; padding: 0;
    height: 100%;
    width: 100%;
}

article.layer {
    position: relative;
    display: table;
    height: 100%;
    width: 100%;
}

article.layer img {
   max-width: 80%;
   max-height: 80%;
}

div.wrap {
    display: table-cell;
    vertical-align: middle;
    text-align: center;
}
Run Code Online (Sandbox Code Playgroud)

我想做的是保持图像始终完整显示。因此,我不希望在调整浏览器窗口大小时将其切断。现在,当我水平调整浏览器窗口的大小时,此方法可以正常工作,但垂直调整其大小时却不能。

这是一个示例:http : //jsbin.com/ugumuj/10/edit#preview

垂直调整浏览器窗口大小时,是否有办法实现相同目的?我很乐意以纯CSS的方式来做,但是我也会使用jQuery或Javascript来做。

有任何想法吗?先感谢您。

the*_*dox 3

javascript:

window.onresize = function(){
      $('article.layer img').css({
        maxHeight: $('article.layer').height() * 0.8,
        maxWidth: $('article.layer').width() * 0.8
      });
    };
Run Code Online (Sandbox Code Playgroud)

jQuery:

$(window).resize(function(){
  $('article.layer img').css({
    maxHeight: $('article.layer').height() * 0.8,
    maxWidth: $('article.layer').width() * 0.8
  });
});
Run Code Online (Sandbox Code Playgroud)

演示版