Javascript 垂直居中可变高度图像,同时保持最大宽度/高度

Javascript 垂直居中可变高度图像,同时保持最大宽度/高度,javascript,css,vertical-alignment,Javascript,Css,Vertical Alignment,我希望将宽度/高度未知的图像置于页面中央,同时确保如果图像大于页面,图像会缩小(即使用max width/max height) 我尝试使用display:table cell方法,但是在Firefox中,对于使用display:table cell声明的元素中的所有元素,都忽略了max width。是否有一种方法可以在不使用display:table cell的情况下使可变高度元素垂直居中 我可以改变加价。JavaScript是可以接受的,但我不能使用JQuery(或任何其他JS库)。试试这样

我希望将宽度/高度未知的图像置于页面中央,同时确保如果图像大于页面,图像会缩小(即使用
max width
/
max height

我尝试使用
display:table cell
方法,但是在Firefox中,对于使用
display:table cell
声明的元素中的所有元素,都忽略了
max width
。是否有一种方法可以在不使用
display:table cell
的情况下使可变高度元素垂直居中


我可以改变加价。JavaScript是可以接受的,但我不能使用JQuery(或任何其他JS库)。

试试这样的方法

演示:

$(函数(){
h=$('.img').height();
w=$(窗口).height();
如果(h

(您可以通过更改我已注释掉的一些CSS来测试不同的大小。)

这应该证明非常有效。。。无需JavaScript:)

CSS
/*不要更改-定位*/
绝对中心{
保证金:自动;
位置:绝对位置;
排名:0;
底部:0;
左:0;
右:0;
}
/*尺寸*/
绝对中心{
最大高度:100%;
最大宽度:100%;
}
HTML


注意:这个类可以很容易地用于任何事情。如果您将其用于图像以外的其他内容,请确保添加一个
标记。absoluteCenter
CSS规则,并选择
最大高度
最大宽度
(其中
标记
是您正在使用的HTML标记[例如
div.absoluteCenter
]并且
最大宽度
/
最大高度
小于
100%
)。

使用javascript有一种方法:

<html>
<head>
<style>
  html, body{
    height:100%;
    margin:0;
    border:0;
    padding:0;
    background:#000;
  }
  body{
    position:relative;
  }
  img{
    border:0;
    padding:0;
    margin:0 auto;
    max-width:100%;
    max-height:100%;
    display:block;
    position:absolute;
  }
</style>
</head>
<body>
  <img />
  <script>
    (function(){
      var imgs = [
          'http://farm3.static.flickr.com/2396/2078094921_ee60c42d0f.jpg',
          'http://farm6.static.flickr.com/5242/5353846171_9169f810dc_b.jpg',
          'http://farm6.static.flickr.com/5284/5336493514_8e41696b66_b.jpg'
        ],
        img = document.getElementsByTagName('IMG')[0],
        getStyle = function(elm){
          return window.getComputedStyle ? window.getComputedStyle(elm) : elm.currentStyle;
        },
        bodyStyle = getStyle(document.body),
        toInt = function(pxSize){
          return +pxSize.replace(/px/,'');
        },
        chgImg = function(){
          img.src = imgs[i++ % imgs.length];
          img.onload = function(){
            var imgStyle = getStyle(img);
            img.style.left = ( toInt(bodyStyle.width)  - toInt(imgStyle.width) ) / 2 + 'px';
            img.style.top =  ( toInt(bodyStyle.height) - toInt(imgStyle.height) ) / 2 + 'px';
            img.onload = null;
          };
        },
        i = 0;

      chgImg();
      setInterval(chgImg, 3000);
    })();
  </script>
</body>
</html>​

html,正文{
身高:100%;
保证金:0;
边界:0;
填充:0;
背景:#000;
}
身体{
位置:相对位置;
}
img{
边界:0;
填充:0;
保证金:0自动;
最大宽度:100%;
最大高度:100%;
显示:块;
位置:绝对位置;
}
​

你确定吗<代码>边距:自动
与宽度配合使用,但与高度配合不太好。至少在我的Chrome测试中是这样。这是一个非常巧妙的留有空白的技巧:自动谢谢!这是我用来测试它的小提琴:@Mic,是的,它和高度配合得很好。诀窍在于位置:绝对;排名:0;底部:0;(左:0;右:0;水平对齐)。哇,这太棒了。我从未在别处见过这种把戏。这是否适用于所有情况,即不只是一个img?@MTsoul-是的,它适用于任何情况。但是,对于块元素,必须设置宽度/高度(它们不会水平收缩到内容)。要使其正常工作,只需添加TAG.absoluteCenter(或者可以使用第二个类[.someClass.absoluteCenter]或id)。然后,添加“最大高度”和“最大宽度”,即可全部设置:)
<html>
<head>
<style>
  html, body{
    height:100%;
    margin:0;
    border:0;
    padding:0;
    background:#000;
  }
  body{
    position:relative;
  }
  img{
    border:0;
    padding:0;
    margin:0 auto;
    max-width:100%;
    max-height:100%;
    display:block;
    position:absolute;
  }
</style>
</head>
<body>
  <img />
  <script>
    (function(){
      var imgs = [
          'http://farm3.static.flickr.com/2396/2078094921_ee60c42d0f.jpg',
          'http://farm6.static.flickr.com/5242/5353846171_9169f810dc_b.jpg',
          'http://farm6.static.flickr.com/5284/5336493514_8e41696b66_b.jpg'
        ],
        img = document.getElementsByTagName('IMG')[0],
        getStyle = function(elm){
          return window.getComputedStyle ? window.getComputedStyle(elm) : elm.currentStyle;
        },
        bodyStyle = getStyle(document.body),
        toInt = function(pxSize){
          return +pxSize.replace(/px/,'');
        },
        chgImg = function(){
          img.src = imgs[i++ % imgs.length];
          img.onload = function(){
            var imgStyle = getStyle(img);
            img.style.left = ( toInt(bodyStyle.width)  - toInt(imgStyle.width) ) / 2 + 'px';
            img.style.top =  ( toInt(bodyStyle.height) - toInt(imgStyle.height) ) / 2 + 'px';
            img.onload = null;
          };
        },
        i = 0;

      chgImg();
      setInterval(chgImg, 3000);
    })();
  </script>
</body>
</html>​