jQuery min/max property from array of elements

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

    Use Fast JavaScript Max/Min - John Resig

    Example with three logos of google, yahoo and bing.

    HTML

    Google Logo
    Yahoo Logo
    Bing Logo

    Javascript

    $(document).ready(function(){
        // Function to get the Max value in Array
        Array.max = function( array ){
            return Math.max.apply( Math, array );
        };
    
        // Function to get the Min value in Array
        Array.min = function( array ){
           return Math.min.apply( Math, array );
        };
    
        //updated as per Sime Vidas comment.
        var widths= $('img').map(function() {
            return $(this).width();
        }).get();
    
        alert("Max Width: " + Array.max(widths));
        alert("Min Width: " + Array.min(widths));
    });
    

    P.S: jsfiddle here

提交回复
热议问题