How might I force a floating DIV to match the height of another floating DIV?

前端 未结 11 647
悲&欢浪女
悲&欢浪女 2020-12-02 04:59

My HTML code is just dividing the pages into two columns, 65%,35% respectively.

11条回答
  •  一向
    一向 (楼主)
    2020-12-02 05:39

    This code will let you have a variable number of rows (with a variable number of DIVs on each row) and it will make all of the DIVs on each row match the height of its tallest neighbour:

    If we assumed all the DIVs, that are floating, are inside a container with the id "divContainer", then you could use the following:

    $(document).ready(function() {
    
    var currentTallest = 0;
    var currentRowStart = 0;
    var rowDivs = new Array();
    
    $('div#divContainer div').each(function(index) {
    
        if(currentRowStart != $(this).position().top) {
    
            // we just came to a new row.  Set all the heights on the completed row
            for(currentDiv = 0 ; currentDiv < rowDivs.length ; currentDiv++) rowDivs[currentDiv].height(currentTallest);
    
            // set the variables for the new row
            rowDivs.length = 0; // empty the array
            currentRowStart = $(this).position().top;
            currentTallest = $(this).height();
            rowDivs.push($(this));
    
        } else {
    
            // another div on the current row.  Add it to the list and check if it's taller
            rowDivs.push($(this));
            currentTallest = (currentTallest < $(this).height()) ? ($(this).height()) : (currentTallest);
    
        }
        // do the last row
        for(currentDiv = 0 ; currentDiv < rowDivs.length ; currentDiv++) rowDivs[currentDiv].height(currentTallest);
    
    });
    });
    

提交回复
热议问题