【问题标题】:How to calculate each child div height and width如何计算每个子div的高度和宽度
【发布时间】:2020-01-13 08:21:53
【问题描述】:

如何计算子 DIV 的高度和宽度。

我想计算每个子 DIV 的高度和宽度,然后比较它的宽度和高度。如果宽度大于高度,则添加 class1 或高度大于宽度,然后添加 class2。并且宽度等于高度然后添加 class3

$(window).load(function() {
  $('.grid').children().each(function(item) {
    var divHeight = 0;
    var divWidth = 0;
    divHeight = $('.grid-item').height();
    divWidth = $('.grid-item').width();
    console.log(divWidth);
    console.log(divHeight);
    //check if child div's width is greater then height then add some class
    if ($(this).width() > $(this).height()) {
      if ($(this).hasClass('class1')) {
        $(this).removeClass('class1');
      } else {
        $(this).addClass('class1');
      }
    }
  });
});
* {
  box-sizing: border-box;
}

body {
  font-family: sans-serif;
}


/* ---- grid ---- */

h1 {
  text-align: center
}

.grid {
  background: #DDD;
  max-width: 1200px;
  margin: 0 auto;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h1>Isotope - masonry layout mode</h1>
<div class="grid">
  <div class="grid-item">
    <img src="https://via.placeholder.com/300/09f/fff.png" alt="">
  </div>
  <div class="grid-item">
    <img src="https://via.placeholder.com/728x90.png" alt="">
  </div>
  <div class="grid-item">
    <img src="https://via.placeholder.com/500x100.png" alt="">
  </div>
</div>

【问题讨论】:

  • 您有控制台错误
  • @mplungjan 是的,感谢编辑!

标签: javascript jquery html css jquery-masonry


【解决方案1】:

如果class="grid" 的孩子总是class="grid-item",最好使用选择器作为$('.grid-item')

  • 还可以使用.outerHeight().outerWidth() 方法计算带边距的准确高度和宽度

$('.grid-item').each(function() {
    let $this = $(this);
    let divHeight = parseFloat($this.outerHeight());
    let divWidth = parseFloat($this.outerWidth());
    let className = "";
    //check if child div's width is greater then height then add some class
    className = divWidth > divHeight ? 'class1' : 'class2';
    if (divWidth === divHeight)
        className = 'class3';
    $this.removeClass('class1').removeClass('class2').removeClass('class3').addClass(className);
});
* {
  box-sizing: border-box;
}

body {
  font-family: sans-serif;
}


/* ---- grid ---- */

h1 {
  text-align: center
}

.grid {
  background: #DDD;
  max-width: 1200px;
  margin: 0 auto;
}
.class1 { background-color: red; }
.class2 { background-color: blue; }
.class3 { background-color: yellow; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h1>Isotope - masonry layout mode</h1>
<div class="grid" >
  <div class="grid-item" style="height:200px;width:200px;" >
    <img src="https://via.placeholder.com/300/09f/fff.png" alt="">
  </div>
  <div class="grid-item">
    <img src="https://via.placeholder.com/728x90.png" alt="">
  </div>
  <div class="grid-item" style="height:203px;width:201px;">
    <img src="https://via.placeholder.com/100x500.png" alt="">
  </div>
</div>

【讨论】:

  • 您的解决方案运行良好。谢谢你的努力兄弟:)。但是在这里你给出了静态的高度和宽度。而我的图像将来自数据库的动态。那时它将“class1”添加到所有子div
  • @HimanshuVaghela 我给出了静态的高度和宽度仅用于演示,但即使您的高度和宽度值也来自数据库,它也能正常工作,您不必担心。
  • 我在我的 codepen 中试过了。请在此处查看。 codepen.io/manshu07/pen/oNgyzrr它在所有子div中添加class3
  • @HimanshuVaghela codepen.io/shoesheill/pen/YzPvpKP 检查它工作正常。
  • 对不起兄弟。这里最新的codepen。我忘记更新最新的了。codepen.io/manshu07/pen/oNgyzrr
【解决方案2】:

使用$('.grid-item').each(function(item) { 代替$('.grid').children().each(function(item) { 并通过$(this).height()width() 查找元素

$('.grid-item').each(function(item) {
  let divHeight = 0;
  let divWidth = 0;
  divHeight = $(this).find('img').height();
  divWidth = $(this).find('img').width();

  if (divWidth > divHeight) {
    $(this).addClass('class1');
  }
});
* {
  box-sizing: border-box;
}

body {
  font-family: sans-serif;
}


/* ---- grid ---- */

h1 {
  text-align: center
}

.grid {
  background: #DDD;
  max-width: 1200px;
  margin: 0 auto;
}

.class1 {
  background: orange
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h1>Isotope - masonry layout mode</h1>
<div class="grid">
  <div class="grid-item">
    <img src="https://via.placeholder.com/300/09f/fff.png" alt="">
  </div>
  <div class="grid-item">
    <img src="https://via.placeholder.com/728x90.png" alt="">
  </div>
  <div class="grid-item">
    <img src="https://via.placeholder.com/500x100.png" alt="">
  </div>
</div>

编辑:根据您的评论,您应该获得图像的宽度和高度,而不是您的 div,当然在这个例子中!

【讨论】:

  • 我尝试了这个解决方案,但它在所有 div 中的添加类不大于高度
【解决方案3】:

也许您需要更具体一些 - 您想检查 div 中的 IMAGE,因为 div 的宽度都相同

$(function() {
  $('.grid .grid-item').each(function() {
    var imgHeight = $(this).find("img").height();
    var imgWidth  = $(this).find("img").width();

    //check if child div's width is greater then height then add some class
    console.log(imgHeight, imgWidth, imgHeight> imgWidth)
    $(this).toggleClass('class1',imgHeight > imgWidth);
  });
});
* {
  box-sizing: border-box;
}

body {
  font-family: sans-serif;
}


/* ---- grid ---- */

h1 {
  text-align: center
}

.grid {
  background: #DDD;
  max-width: 1200px;
  margin: 0 auto;
}
.class1 { background-color:red }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h1>Isotope - masonry layout mode</h1>
<div class="grid">
  <div class="grid-item">
    <img src="https://via.placeholder.com/300/09f/fff.png" alt="">
  </div>
  <div class="grid-item">
    <img src="https://via.placeholder.com/728x90.png" alt="">
  </div>
  <div class="grid-item">
    <img src="https://via.placeholder.com/100x500.png" alt="">
  </div>
</div>

【讨论】:

  • 我试过这个解决方案,但如果高度大于宽度,它不会添加类。
  • 谢谢哥们!!我不确定为什么它在我的代码中不起作用。让我再试一次。
猜你喜欢
  • 2012-11-04
  • 2022-10-18
  • 1970-01-01
  • 2015-12-16
  • 2016-01-14
  • 1970-01-01
  • 2014-11-21
  • 2014-11-30
  • 1970-01-01
相关资源
最近更新 更多