如何在固定高度的div内垂直居中动态高度的图像?

时间:2015-05-23 07:22:36

标签: javascript jquery html css

我正在创建一个图像库,其中包含固定高度和宽度div的可变高度图像。我的父div的高度是460px。我希望所有小于460px高度的图像在div的中心垂直对齐。

这是我的js代码:

$(document).ready(function(){
        var elemst = document.getElementsByName("pictureDiv");
        var img = $('.x-simple img');
        var parentdiv = $('.x-simple');
        for (var i=0;i<elemst.length;i++)
        {
            var ph = parentdiv.height();
            var h = img.height();
            console.log('img height' + h);
            var mh = Math.ceil((ph - h) / 2);

            if(mh>0){
                $('.x-simple img').css('margin-top', mh);
            }
            else
            {
            $('.x-simple img').css('margin-top', 0);
            }
        }
        });

我的HTML:

<div class="col-md-12">
<div class="col-md-3 col-xs-12 marg-bottom main-img-wrap">
    <div class="xoverlay x-simple" name="pictureDiv">
        <a href="#" target="_blank">
                <img class="x-img-main" src="pic1.jpg">
        </a>
    </div>
</div>
<div class="col-md-3 col-xs-12 marg-bottom main-img-wrap">
    <div class="xoverlay x-simple" name="pictureDiv">
        <a href="#" target="_blank">
                <img class="x-img-main" src="pic2.jpg">
        </a>
    </div>
</div>
<div class="col-md-3 col-xs-12 marg-bottom main-img-wrap">
    <div class="xoverlay x-simple" name="pictureDiv">
        <a href="#" target="_blank">
                <img class="x-img-main" src="pic3.jpg">
        </a>
    </div>
</div>
<div class="col-md-3 col-xs-12 marg-bottom main-img-wrap">
    <div class="xoverlay x-simple" name="pictureDiv">
        <a href="#" target="_blank">
                <img class="x-img-main" src="pic4.jpg">
        </a>
    </div>
</div>

我的代码的问题是,如果我执行console.log('img height'+ h),它只给我一个473px的常量高度;这是我第一张照片的高度。 此外,我可以看到“margin-top:0px”在我的所有图片中添加,无论它们的高度如何。高度较小的图像也会添加“margin-top:0px”,尽管它不应该发生。不知道我的代码在哪里犯了错误。

1 个答案:

答案 0 :(得分:1)

您可以使用each()对其进行迭代

&#13;
&#13;
$(document).ready(function() {

  $('.x-simple').each(function() {
    var $this = $(this),
        $img = $this.find('.x-img-main'),
        ph = $this.height(),
        h = $img.height(),
        mh = Math.ceil((ph - h) / 2);
    if (mh > 0) {
      $img.css('margin-top', mh);
    } else {
      $img.css('margin-top', 0);
    }
  });
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="col-md-12">
  <div class="col-md-3 col-xs-12 marg-bottom main-img-wrap">
    <div class="xoverlay x-simple" name="pictureDiv">
      <a href="#" target="_blank">
        <img class="x-img-main" src="pic1.jpg">
      </a>
    </div>
  </div>
  <div class="col-md-3 col-xs-12 marg-bottom main-img-wrap">
    <div class="xoverlay x-simple" name="pictureDiv">
      <a href="#" target="_blank">
        <img class="x-img-main" src="pic2.jpg">
      </a>
    </div>
  </div>
  <div class="col-md-3 col-xs-12 marg-bottom main-img-wrap">
    <div class="xoverlay x-simple" name="pictureDiv">
      <a href="#" target="_blank">
        <img class="x-img-main" src="pic3.jpg">
      </a>
    </div>
  </div>
  <div class="col-md-3 col-xs-12 marg-bottom main-img-wrap">
    <div class="xoverlay x-simple" name="pictureDiv">
      <a href="#" target="_blank">
        <img class="x-img-main" src="pic4.jpg">
      </a>
    </div>
  </div>
&#13;
&#13;
&#13;