jQuery min/max property from array of elements

前端 未结 8 1450
陌清茗
陌清茗 2020-11-29 02:09

Is there a simple way to find the min/max property from an array of elements in jQuery?

I constantly find myself dynamically resizing groups of elements based on the

相关标签:
8条回答
  • 2020-11-29 02:36

    You can use apply outside the context of OO, no need to extend the prototype:

    var maxHeight = Math.max.apply( null,
            $('img').map(function(){ return $(this).height(); }).get() );
    
    0 讨论(0)
  • 2020-11-29 02:37

    I like the elegant solution posted as a .map() example in the jQuery docs on how to equalize div heights. I basically adapted it to work with widths and made a demo.

    $.fn.limitWidth = function(max){
      var limit = (max) ? 'max' : 'min';
      return this.width( Math[limit].apply(this, $(this).map(function(i,e){
       return $(e).width();
      }).get() ) );
    };
    
    // Use the function above as follows
    $('.max-width').limitWidth(true); // true flag means set to max
    $('.min-width').limitWidth();     // no flag/false flag means set to min
    
    0 讨论(0)
提交回复
热议问题