简体   繁体   English

在动态div中垂直对齐图像

[英]Vertically align image in dynamic div

I am trying to vertically align the images on this page but have had no luck. 我试图垂直对齐此页面上的图像,但没有运气。

I need them centered to the text block. 我需要它们以文本块为中心。 But only when the page is wide enough that the images are shown next to the text. 但是仅当页面足够宽时,图像才会显示在文本旁边。

Link to demo page: http://ruigendyk.com/static/stackoverflow/questions/1/ 链接到演示页面: http : //ruigendyk.com/static/stackoverflow/questions/1/

There's a few things you need to do... 您需要做几件事...

Add the following css to .img-frame 将以下CSS添加到.img-frame

height:100%;

Then the following .featurette-image 然后下面的.featurette-image

position: relative;
top: 50%;
left: 50%;
-webkit-transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);

For the vertical align css to work you then need your image column to match the height of your text column, you can do that using the following script: 为了使垂直对齐css正常工作,您需要使image列与text列的高度匹配,可以使用以下脚本进行操作:

equalheight = function(container){
var currentTallest = 0,
     currentRowStart = 0,
     rowDivs = new Array(),
     $el,
     topPosition = 0;
 $(container).each(function() {

   $el = $(this);
   $($el).height('auto')
   topPostion = $el.position().top;

   if (currentRowStart != topPostion) {
     for (currentDiv = 0 ; currentDiv < rowDivs.length ; currentDiv++) {
       rowDivs[currentDiv].height(currentTallest);
     }
     rowDivs.length = 0; // empty the array
     currentRowStart = topPostion;
     currentTallest = $el.height();
     rowDivs.push($el);
   } else {
     rowDivs.push($el);
     currentTallest = (currentTallest < $el.height()) ? ($el.height()) : (currentTallest);
  }
   for (currentDiv = 0 ; currentDiv < rowDivs.length ; currentDiv++) {
     rowDivs[currentDiv].height(currentTallest);
   }
 });
}

$(window).load(function() {
  equalheight('.featurette div');
});
$(window).resize(function(){
  equalheight('.featurette div');
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM