最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get element's height and apply CSS to different elements based on height? - Stack Overflow

programmeradmin5浏览0评论

What I am trying to do is to get the heights of multiple elements with the same 'class'. If the element's height is over 400px I want it to set the height to the value it aquired minus 5px. And if it is under 400px I want it to do the same thing.

jQuery(document).ready(function( $ ){
  
  var cardHeight = $('.card').height();
  
  if (cardHeight > 400) {
    $(this).height(cardHeight - 5);
    }
  else{
    $(this).height(cardHeight - 5);
  }
  
}); 
.wrapper {
  width: 100%;
  overflow: hidden;
}
.wrapper .card {
  width: 20%;
  float: left;
  margin: 10px;
  background-color: red;
}
.wrapper .card:nth-child(1) {
  height: 333px;
}
.wrapper .card:nth-child(2) {
  height: 333px;
}
.wrapper .card:nth-child(3) {
  height: 500px;
}
.wrapper .card:nth-child(4) {
  height: 500px;
}
<script src=".7.1/jquery.min.js"></script>
<body>
<div class="wrapper">
  <div class="card"></div>
  <div class="card"></div>
  <div class="card"></div>
  <div class="card"></div>
</div>
</body>

What I am trying to do is to get the heights of multiple elements with the same 'class'. If the element's height is over 400px I want it to set the height to the value it aquired minus 5px. And if it is under 400px I want it to do the same thing.

jQuery(document).ready(function( $ ){
  
  var cardHeight = $('.card').height();
  
  if (cardHeight > 400) {
    $(this).height(cardHeight - 5);
    }
  else{
    $(this).height(cardHeight - 5);
  }
  
}); 
.wrapper {
  width: 100%;
  overflow: hidden;
}
.wrapper .card {
  width: 20%;
  float: left;
  margin: 10px;
  background-color: red;
}
.wrapper .card:nth-child(1) {
  height: 333px;
}
.wrapper .card:nth-child(2) {
  height: 333px;
}
.wrapper .card:nth-child(3) {
  height: 500px;
}
.wrapper .card:nth-child(4) {
  height: 500px;
}
<script src="https://cdnjs.cloudflare/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<body>
<div class="wrapper">
  <div class="card"></div>
  <div class="card"></div>
  <div class="card"></div>
  <div class="card"></div>
</div>
</body>

Share Improve this question edited Apr 1 at 5:55 DarkBee 15.5k8 gold badges72 silver badges117 bronze badges asked Apr 1 at 0:21 agon024agon024 1,0171 gold badge14 silver badges37 bronze badges 1
  • 1 Please edit your question to contain a minimal reproducible example. Links to code on other sites are not appropriate. Please also describe the problem in terms of what you expect to happen vs what actually happens – Phil Commented Apr 1 at 0:23
Add a comment  | 

2 Answers 2

Reset to default 4

In the original version of the code, the problem is in the moment when height() is taken immediately. The value of cardHeight = the height of the 1st element with the card class, you can verify this by setting the following value in the styles:

 &:nth-child(1) {
      height: 111px;
    }
    &:nth-child(2) {
      height: 222px;
    }
    &:nth-child(3) {
      height: 333px;
    }
    &:nth-child(4) {
      height: 444px;
    }

cardHeight will be equal to 111px in this case.

If you need to go through several elements with the same classes, you can use each. And then do the necessary manipulations with each element.

jQuery(document).ready(function($) {
  $('.card').each(function(index, element) {
    var cardHeight = $(element).height();
    console.log(cardHeight);

    if (cardHeight > 400) {
      $(element).height(cardHeight - 5);
    } else {
      $(element).height(cardHeight - 5);
    }
  });
});

The if/else conditions themselves are redundant in this case.

jQuery(document).ready(function($) {
  $('.card').each(function(index, element) {
    var cardHeight = $(element).height();
    console.log(cardHeight);

    $(element).height(cardHeight - 5);
  });
});

I can see two problems in your approach:

  1. The height assigned to the cardHeight variable is the height of the first element with class as card. In order to capture each and every element with class card, you should use the $.each() function of jQuery.

  2. The if/else conditions have same statement inside them, so you can omit it.

So, the correct code would be:

jQuery(document).ready(function($) {
  $('.card').each(function(index, ele) {
    var cardHeight = $(ele).height();
    $(element).height(cardHeight - 5);
  });
});

Hope this helps.

发布评论

评论列表(0)

  1. 暂无评论