简体   繁体   English

具有流体宽度和高度的行中的等高 div(或 li)

[英]Equal height divs (or li) in rows with fluid width and height

I found this sweet jQuery snippet by CSS-Tricks' Chris Coyier that resets div elements heights that share the same top position on the page (are on the same row) to the tallest element.我发现 CSS-Tricks 的 Chris Coyier 编写的这个甜蜜的 jQuery 片段,它将在页面上共享相同顶部位置(在同一行上)的 div 元素高度重置为最高元素。

The Problem问题

This solution almost works with fluid width layouts and resets height when top positions changes but it resets it to the original height of the current tallest element in the row when the page first loaded.此解决方案几乎适用于流体宽度布局,并在顶部位置更改时重置高度,但在页面首次加载时将其重置为行中当前最高元素的原始高度。 This is an issue because the height of the tallest element might have changed since this page first loaded because of the use of relative units like ems or because of word wrapping with paragraphs.这是一个问题,因为自从首次加载此页面以来,由于使用了相对单位(如 ems)或由于段落自动换行,最高元素的高度可能已经发生了变化。

Proposed Solution建议的解决方案

The solution would be to have the row's elements' height being set to the tallest element's current height, not the original height.解决方案是将行元素的高度设置为最高元素的当前高度,而不是原始高度。 I have been unsuccessful in accomplishing this.我一直没有成功地做到这一点。

Here is the snippet where "li.half" is the elements being compared and resized.这是“li.half”是被比较和调整大小的元素的片段。

jQuery(document).ready(function($) {
// these are (ruh-roh) globals. You could wrap in an
    // immediately-Invoked Function Expression (IIFE) if you wanted to...
    var currentTallest = 0,
        currentRowStart = 0,
        rowDivs = new Array();
    
    function setConformingHeight(el, newHeight) {
        // set the height to something new, but remember the original height in case things change
        el.data("originalHeight", (el.data("originalHeight") == undefined) ? (el.height()) : (el.data("originalHeight")));
        el.height(newHeight);
    }
    
    function getOriginalHeight(el) {
        // if the height has changed, send the originalHeight
        return (el.data("originalHeight") == undefined) ? (el.height()) : (el.data("originalHeight"));
    }
    
    function columnConform() {
    
        // find the tallest DIV in the row, and set the heights of all of the DIVs to match it.
        $('li.half').each(function() {
        
            // "caching"
            var $el = $(this);
            
            var topPosition = $el.position().top;
    
            if (currentRowStart != topPosition) {
    
                // we just came to a new row.  Set all the heights on the completed row
                for(currentDiv = 0 ; currentDiv < rowDivs.length ; currentDiv++) setConformingHeight(rowDivs[currentDiv], currentTallest);
    
                // set the variables for the new row
                rowDivs.length = 0; // empty the array
                currentRowStart = topPosition;
                currentTallest = getOriginalHeight($el);
                rowDivs.push($el);
    
            } else {
    
                // another div on the current row.  Add it to the list and check if it's taller
                rowDivs.push($el);
                currentTallest = (currentTallest < getOriginalHeight($el)) ? (getOriginalHeight($el)) : (currentTallest);
    
            }
            // do the last row
            for (currentDiv = 0 ; currentDiv < rowDivs.length ; currentDiv++) setConformingHeight(rowDivs[currentDiv], currentTallest);
    
        });
    }
    
    
    $(window).resize(function(){
        columnConform();
    });
    
    // Dom Ready
    // You might also want to wait until window.onload if images are the things that
    // are unequalizing the blocks
    $(function() {
        columnConform();
    });
});

Please let me know if you can figure out how to make the setConformingHeight adjust on window resize.如果您能弄清楚如何在调整窗口大小时调整 setConformingHeight,请告诉我。

Just found this on CodePen from Micah Godbolt and it seems to be working a charm. 刚刚在Micah Godbolt的CodePen上找到了它,似乎很有吸引力。 It's borrowed from Chris Coyier, who borrowed from Stephen Akins. 它是从Chris Coyier借来的,而Chris Coyier是从Stephen Akins借来的。 It was actually the search result under this page for "jquery equal height row by row." 它实际上是此页面下“ jquery等高逐行”的搜索结果。

http://codepen.io/micahgodbolt/pen/FgqLc http://codepen.io/micahgodbolt/pen/FgqLc

Those solutions didn't work on window.resize() as elements height should be unlocked with $el.height('auto') before calculating new real height.这些解决方案在 window.resize() 上不起作用,因为在计算新的实际高度之前,应该使用 $el.height('auto') 解锁元素高度。

Here is my solution :这是我的解决方案:

var currentRowTop = -100, currentHighest=  0;

$('.page-wrapper .cc').each(function() {
    $el=$(this);
    if($el.position().top!=currentRowTop){
        equalizeHeight();
        currentRowTop = $el.position().top; 
        $el.height('auto').addClass('same-height');
        currentHighest=$el.height();
    }else{
        $el.height('auto').addClass('same-height');
        currentHighest = ($el.height()>currentHighest) ? $el.height() : currentHighest ;
    }
});
equalizeHeight();   

function equalizeHeight(){
    if($('.same-height').size()==0) return;
    $('.same-height').height(currentHighest).removeClass('same-height');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM