【问题标题】:element with the max height from a set of elements一组元素中具有最大高度的元素
【发布时间】:2011-08-28 23:51:07
【问题描述】:

我有一组div 元素。在jQuery 中,我希望能够找出具有最大高度的div 以及该div 的高度。例如:

<div>
    <div class="panel">
      Line 1
      Line 2
    </div>
    <div class="panel">
      Line 1<br/>
      Line 2<br/>
      Line 3<br/>
      Line 4<br/>
    </div>
    <div class="panel">
      Line 1
    </div>
    <div class="panel">
      Line 1
      Line 2
    </div>
&lt/div>

通过查看上面的内容,我们知道第二个div(有 4 行)具有最大的高度。我怎么知道这个?有人可以帮忙吗?

到目前为止我已经尝试过:

$("div.panel").height() 返回第一个div 的高度。

【问题讨论】:

    标签: jquery


    【解决方案1】:

    这在某种程度上可能会有所帮助。修复来自@diogo 的一些代码

    $(window).on('load',function(){
    
    // get the maxHeight using innerHeight() function
      var maxHeight = Math.max.apply(null, $("div.panel").map(function ()                                                        {
        return $(this).innerHeight();
      }).get());
      
    // Set the height to all .panel elements
      $("div.panel").height( maxHeight );
      
    });
    

    【讨论】:

      【解决方案2】:

      我想说的最简单和最清晰的方法是:

      var maxHeight = 0, maxHeightElement = null;
      $('.panel').each(function(){
         if ($(this).height() > maxHeight) {
             maxHeight = $(this).height();
             maxHeightElement = $(this);
         }
      });
      

      【讨论】:

        【解决方案3】:

        使用.map()Math.max

        var maxHeight = Math.max.apply(null, $("div.panel").map(function ()
        {
            return $(this).height();
        }).get());
        

        如果读起来令人困惑,这可能会更清楚:

        var heights = $("div.panel").map(function ()
            {
                return $(this).height();
            }).get();
        
        maxHeight = Math.max.apply(null, heights);
        

        【讨论】:

        • 这会找到最高高度,但不是对实际元素的引用。
        • 你是对的;我不清楚 OP 是否也想要这个元素。您的答案在这种情况下有效。
        • @MattBall 为什么是 get 函数,请问?它有什么作用,它的目的是什么?
        • @chodorowicz 很高兴你问到。请参阅第二个标题下的this answer
        • @MattBall 谢谢。所以jQuery .map 返回类似数组的对象,get .get 把它变成数组。但似乎 Math.max 在这两种类型上都很容易工作(刚刚在 Chrome 控制台中测试过),所以在这种情况下 .get 似乎没有必要。还是我错了?
        【解决方案4】:

        ul, li {
          list-style: none;
          margin: 0;
          padding: 0;
        }
        
        ul {
          display: flex;
          flex-wrap: wrap;
        }
        
        ul li {
          width: calc(100% / 3);
        }
        
        img {
          width: 100%;
          height: auto;
        }
        <ul>
          <li>
            <img src="http://img2.vetton.ru//upl/1000/346/138/vetton_ru_sddu7-2560x1600.jpg" alt="">
            <br> Line 1
            <br> Line 2
          </li>
          <li>
            <img src="http://img2.vetton.ru//upl/1000/346/138/vetton_ru_mixwall66-2560x1600.jpg" alt="">
            <br> Line 1
            <br> Line 2
            <br> Line 3
            <br> Line 4
          </li>
          <li>
            <img src="http://img2.vetton.ru//upl/1000/346/138/vetton_ru_sddu7-2560x1600.jpg" alt="">
            <br> Line 1
          </li>
          <li>
            <img src="http://img2.vetton.ru//upl/1000/346/138/vetton_ru_mixwall66-2560x1600.jpg" alt="">
            <br> Line 1
            <br> Line 2
          </li>
        </ul>

        【讨论】:

          【解决方案5】:

          如果您对完全使用标准 JavaScript 或不使用 forEach() 进行排序感兴趣:

          var panels = document.querySelectorAll("div.panel");
          
          // You'll need to slice the node_list before using .map()
          var heights = Array.prototype.slice.call(panels).map(function (panel) {
              // return an array to hold the item and its value
              return [panel, panel.offsetHeight];
          }),
          
          // Returns a sorted array
          var sortedHeights = heights.sort(function(a, b) { return a[1] > b[1]});
          

          【讨论】:

            【解决方案6】:

            如果你想在多个地方重复使用:

            var maxHeight = function(elems){
                return Math.max.apply(null, elems.map(function ()
                {
                    return $(this).height();
                }).get());
            }
            

            那么你可以使用:

            maxHeight($("some selector"));
            

            【讨论】:

              【解决方案7】:

              function startListHeight($tag) {
                
                  function setHeight(s) {
                      var max = 0;
                      s.each(function() {
                          var h = $(this).height();
                          max = Math.max(max, h);
                      }).height('').height(max);
                  }
                
                  $(window).on('ready load resize', setHeight($tag));
              }
              
              jQuery(function($) {
                  $('#list-lines').each(function() {
                      startListHeight($('li', this));
                  });
              });
              #list-lines {
                  margin: 0;
                  padding: 0;
              }
              
              #list-lines li {
                  float: left;
                  display: table;
                  width: 33.3334%;
                  list-style-type: none;
              }
              
              #list-lines li img {
                  width: 100%;
                  height: auto;
              }
              
              #list-lines::after {
                  content: "";
                  display: block;
                  clear: both;
                  overflow: hidden;
              }
              <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
              
              <ul id="list-lines">
                  <li>
                      <img src="http://img2.vetton.ru//upl/1000/346/138/vetton_ru_sddu7-2560x1600.jpg" alt="" />
                      <br /> Line 1
                      <br /> Line 2
                  </li>
                  <li>
                      <img src="http://img2.vetton.ru//upl/1000/346/138/vetton_ru_mixwall66-2560x1600.jpg" alt="" />
                      <br /> Line 1
                      <br /> Line 2
                      <br /> Line 3
                      <br /> Line 4
                  </li>
                  <li>
                      <img src="http://img2.vetton.ru//upl/1000/346/138/vetton_ru_sddu7-2560x1600.jpg" alt="" />
                      <br /> Line 1
                  </li>
                  <li>
                      <img src="http://img2.vetton.ru//upl/1000/346/138/vetton_ru_mixwall66-2560x1600.jpg" alt="" />
                      <br /> Line 1
                      <br /> Line 2
                  </li>
              </ul>

              【讨论】:

                【解决方案8】:

                您发布的 html 应该使用一些 &lt;br&gt; 来实际具有不同高度的 div。像这样:

                <div>
                    <div class="panel">
                      Line 1<br>
                      Line 2
                    </div>
                    <div class="panel">
                      Line 1<br>
                      Line 2<br>
                      Line 3<br>
                      Line 4
                    </div>
                    <div class="panel">
                      Line 1
                    </div>
                    <div class="panel">
                      Line 1<br>
                      Line 2
                    </div>
                </div>
                

                除此之外,如果您想要引用具有最大高度的 div,您可以这样做:

                var highest = null;
                var hi = 0;
                $(".panel").each(function(){
                  var h = $(this).height();
                  if(h > hi){
                     hi = h;
                     highest = $(this);  
                  }    
                });
                //highest now contains the div with the highest so lets highlight it
                highest.css("background-color", "red");
                

                【讨论】:

                • 小心这个,如果你没有在内部使用 jQuery 方法(比如 .height 在这种情况下),那么你必须取消引用它,例如:` $(this)[0].滚动高度;`
                【解决方案9】:

                试试Find out divs height and setting div height (与 Matt 发布的类似,但使用循环)

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 2019-03-19
                  • 2017-03-23
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多