तत्वों के एक सेट से अधिकतम ऊंचाई के साथ तत्व


85

मेरे पास 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>
</div>

उपरोक्त को देखकर, हम जानते हैं कि 2 div(4 लाइनों के साथ) सभी की अधिकतम ऊंचाई है। मुझे यह कैसे पता चलेगा? क्या कोई मदद कर सकता है?

अब तक मैंने कोशिश की है:

$("div.panel").height()जो 1 की ऊंचाई देता है div

जवाबों:


213

का उपयोग करें .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);

1
यह उच्चतम ऊंचाई पाता है लेकिन वास्तविक तत्व का संदर्भ नहीं है।
विंसेंट रामधनी

आप सही हे; मैं स्पष्ट नहीं था कि ओपी तत्व भी चाहता है। आपका जवाब उस मामले में काम करता है।
मैट बॉल

3
@MattBall क्यों फ़ंक्शन मिलता है, क्या मैं पूछ सकता हूं? यह क्या करता है और इसका उद्देश्य क्या है?
चोडोरोविक्ज़

2
@chodorowicz खुशी से आपने पूछा। दूसरे शीर्ष के तहत, इस उत्तर को देखें ।
मैट बॉल

1
@MattBall साभार तो jQuery .मैप वस्तु की तरह सरणी लौटाता है और .get इसे सरणी में बदल देता है। लेकिन ऐसा लगता है कि Math.max इन दोनों प्रकारों पर काम करता है (बस क्रोम कंसोल में परीक्षण किया गया है), इसलिए इस मामले में .get अनावश्यक लगता है। या मैं गलत हूँ?
चोडोरोविक्ज़ सिप

20

जिस html को आपने पोस्ट किया है, <br>उसे वास्तव में विभिन्न ऊंचाइयों के साथ कुछ का उपयोग करना चाहिए । इस कदर:

<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");

इस एक के साथ सावधान रहें, यदि आप अंदर ( .heightइस मामले में) की तरह एक jQuery विधि का उपयोग नहीं करते हैं, तो आपको इसे आवश्यक रूप से लागू करना होगा, पूर्व: `$ (यह) [0] .scrollHeight;`
rogerdpack

5

यदि आप कई स्थानों पर पुन: उपयोग करना चाहते हैं:

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

तो आप का उपयोग कर सकते हैं:

maxHeight($("some selector"));

1

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>


1

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>



0

यदि आप पूरी तरह से मानक जावास्क्रिप्ट में छंटनी में रुचि रखते थे, या 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]});

0

सबसे आसान और स्पष्ट तरीका मैं कहूंगा:

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

0

यह किसी तरह से मददगार हो सकता है। @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 );
  
});
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.