【问题标题】:Equal height divs (or li) in rows with fluid width and height (90% finished)行中等高的 div(或 li),具有可变的宽度和高度(已完成 90%)
【发布时间】:2015-07-28 22:03:04
【问题描述】:

嘿,所以我发现了 CSS-tricks Chris Coyier 的这个甜蜜的 jquery sn-p,它将在页面上共享相同顶部位置(在同一行上)的 div 元素高度重置为最高元素。

问题 此解决方案几乎适用于流体宽度布局,并在顶部位置更改时重置高度,但在页面首次加载时将其重置为行中当前最高元素的原始高度。这是一个问题,因为自从首次加载此页面以来,由于使用了相对单位(如 ems)或由于段落自动换行,最高元素的高度可能已经发生了变化。 建议的解决方案 解决方案是将行元素的高度设置为最高元素的当前高度,而不是原始高度。我没有成功。

这里是 sn-p,其中“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();
    });
});

如果您能弄清楚如何在调整窗口大小时调整 setConformingHeight,请告诉我。

谢谢!

【问题讨论】:

    标签: jquery responsive-design fluid-layout equal-heights


    【解决方案1】:

    刚刚在 Micah Godbolt 的 CodePen 上发现了这个,它似乎很有魅力。它是从 Chris Coyier 那里借来的,后者是从 Stephen Akins 那里借来的。其实就是这个页面下的“jquery equal height row by row”的搜索结果。

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

    【讨论】:

    • 欢迎来到 Stack Overflow!虽然这在理论上可以回答问题,it would be preferable 在此处包含答案的基本部分,并提供链接以供参考。
    • 以上链接失效。这是来自 css-tricks.com css-tricks.com/equal-height-blocks-in-rows 的修改版本
    • codepen 链接对我有效,但脚本无效。它处理第一行,然后不遍历其余行,即使它看起来应该这样做。
    • 控制台报错了吗?有时它不能很好地与其他插件一起使用。另外,请确保您的选择器是正确的。我在使用此脚本时遇到的一个问题是它没有向下钻取。例如,您可以将 li 元素的高度设置为相等,但不能将每个 li 元素的 div 子元素的高度设置为相等。该脚本仅适用于兄弟姐妹。我正在为一个可以深入研究的项目编写修改后的脚本,但我还没有开始着手。如果我得到它的工作,我会发布代码。
    【解决方案2】:

    这些解决方案不适用于 window.resize(),因为在计算新的实际高度之前,应使用 $el.height('auto') 解锁元素高度。

    这是我的解决方案:

    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');
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-03
      • 1970-01-01
      • 2020-12-31
      相关资源
      最近更新 更多