如何使块的高度相等

时间:2017-04-20 17:54:27

标签: javascript html

我有这样的html,并希望在不同的行中创建块“.equal-height”相同的高度。

<div class="container">
  <div class="row">
    <div class="col-sm-3 col-md-4 equal-height">Some different by height content</div>
    <div class="col-sm-3 col-md-4 equal-height">Some different by height content</div>
    <div class="col-sm-3 col-md-4 equal-height">Some different by height content</div>
    <div class="col-sm-3 col-md-4 equal-height">Some different by height content</div>
    <div class="col-sm-3 col-md-4 equal-height">Some different by height content</div>
    <div class="col-sm-3 col-md-4 equal-height">Some different by height content</div>
    <div class="col-sm-3 col-md-4 equal-height">Some different by height content</div>
    <div class="col-sm-3 col-md-4 equal-height">Some different by height content</div>
    <div class="col-sm-3 col-md-4 equal-height">Some different by height content</div>
    <div class="col-sm-3 col-md-4 equal-height">Some different by height content</div>
    <div class="col-sm-3 col-md-4 equal-height">Some different by height content</div>
    <div class="col-sm-3 col-md-4 equal-height">Some different by height content</div>
  </div>
</div>

我有脚本,但结果是这样的

var EqualBlocks = document.querySelectorAll(".equal-height");
var equalBlocksLength = EqualBlocks.length;
var maxHeight = 0;

for(var i = 0; i < equalBlocksLength; i++){
  var height = EqualBlocks[i].offsetHeight;
  if(maxHeight < height) maxHeight = height;
  var top = 0;
  var elem = EqualBlocks[i];
  while(elem){
    top += elem.offsetTop;
    elem = elem.offsetParent;
}

wrong equal blocks

当我只在一行中需要相等的块时,就像这样。

equal blocks

任何想法或某些信息来源?

  

而且,我需要纯粹的js,但没有jquery!

0 个答案:

没有答案