jQuery min/max property from array of elements

前端 未结 8 1469
陌清茗
陌清茗 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: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
    

提交回复
热议问题