简体   繁体   English

流体宽度元素的等高行

[英]Equal Height Rows for Fluid Width Elements

I've used Chris Coyiers Equal Height Blocks in Rows jQuery script many times before, and it's always worked great. 我以前曾多次使用过Chris jQuery脚本中的Chris Coyiers Equal Height Blocks ,而且它总是很棒。

That being said, I've always developed websites built for a specific resolution. 话虽这么说,我总是开发为特定分辨率而构建的网站。 This time around I'm developing a responsive website and main container has a fluid width. 这次我正在开发一个响应式网站,主容器有一个流畅的宽度。 As such, I've encountered an issue and can't find a solution to my problem. 因此,我遇到了一个问题,无法找到问题的解决方案。

In his post, Chris says this about fluid widths: 在他的帖子中,克里斯说这关于流体宽度:

What About Fluid Width? 流体宽度怎么样? One of the main points of not using a table for this is that floated divs can rearrange themselves based on the available horizontal space, which is nice for fluid width. 不使用表格的一个要点是浮动的div可以根据可用的水平空间重新排列,这对于流体宽度很好。 We can adjust the above code to deal with that as well. 我们也可以调整上面的代码来处理它。 Basically the first time this is run we'll measure the height of each block and remember it with jQuery's data() method. 基本上第一次运行时,我们将测量每个块的高度,并使用jQuery的data()方法记住它。 Then when the window is resized, run the code again but use the original size to adjust the rows, not the new size. 然后,在调整窗口大小时,再次运行代码,但使用原始大小来调整行,而不是新大小。

Note that he's specifically referencing a fluid width container. 请注意,他特别引用了一个流体宽度的容器。 In my case it's not just the container, but the child elements that also have fluid widths. 在我的例子中,它不仅仅是容器,还有具有流体宽度的子元素。 The very blocks that I need to equalize are the same blocks have fluid widths and adjust to the resolution of the window/container. 我需要均衡的块是相同的块具有流体宽度并调整到窗口/容器的分辨率。

I believe that because the element's 'originalHeight' changes with the container size, the script will no longer work properly. 我相信因为元素'originalHeight'随容器大小而变化,脚本将无法正常工作。

My speculation may be wrong, but one way or another this isn't working with fluid width elements. 我的猜测可能是错误的,但不管怎样,这与流体宽度元素无关。 Looking for some help! 寻求一些帮助!

Of course, here's a jsfiddle I created to demonstrate the problem. 当然,这是我创建的一个jsfiddle来演示这个问题。 Just resize the width of the window and you'll notice the heights of the containers do not update. 只需调整窗口的宽度,您就会注意到容器的高度不会更新。

I'm running the following code to call the function on the window resize event: 我正在运行以下代码来调用窗口resize事件上的函数:

$(window).resize(function() {
    columnConform();
});

Thanks in advance 提前致谢

You're only equalizing the heights of columns that exist on one row so you could replace all that JS with this 您只是平衡一行中存在的列的高度,因此您可以用此替换所有JS

$.fn.extend({
    equalHeights: function(options){
      var ah=(Math.max.apply(null, $(this).map(function(){ return $(this).height(); }).get()));
      if (typeof ah=='number') $(this).height(ah);
    }
 });

and execute like so 并执行如此

$(document).ready(function() {
    $(window).resize(function() {
        $('.equalize').equalHeights();
    }).trigger('resize'); // don't write code twice just to execute on page load - trigger the event you just bound to instead

})

But if you want to do it per pseudo row working demo 但是如果你想按伪行工作演示来做

<ul class="eqme">
  <li><h1>One</h1></li>
  <li><h2>Two</h2></li>
  ....
</ul>

JS JS

$.fn.extend({
equalHeights: function(){
    var top=0;
    var classname=('equalHeights'+Math.random()).replace('.','');
    $(this).each(function(){
      if ($(this).is(':visible')){
        var thistop=$(this).offset().top;
        if (thistop>top) {
            $('.'+classname).removeClass(classname); 
            top=thistop;
        }
        $(this).addClass(classname);
        $(this).height('auto');
        var h=(Math.max.apply(null, $('.'+classname).map(function(){ return $(this).outerHeight(); }).get()));
        $('.'+classname).height(h);
      }
    }).removeClass(classname); 
}       

});

$(function(){
  $(window).resize(function(){
    $('.eqme li').equalHeights();
  }).trigger('resize');
});

This will even take care of many elements that are the children of the same parent even if they break row, eg a ul containing 100 li elements when only 10 fit on one row, or when the li width is static but the page width is not, on resize they'll clear onto new lines when the window is shrunk (or float back to the one above if expanding the width) then their heights will fix to suit the tallest per pseudo row. 这甚至会照顾许多同一父元素的元素,即使它们会破坏行,例如当只有10个元素适合一行时,或者当li宽度是静态但页面宽度不是100时,包含100个li元素的ul ,在调整大小时,当窗口收缩时它们会清除到新线上(或者如果扩展宽度则浮回到上面的那条线),那么它们的高度将固定为适合每个伪行的最高线。

Notes: 笔记:

Feedback suggests that outerHeight() works better than .height() in some situations. 反馈表明在某些情况下.height()outerHeight()效果更好。

In production, within the window.resize function, I added a clearTimeout and setTimeout of 100ms before reacting to the change of size to prevent slowdown when resizing by dragging a corner. 在生产中,在window.resize函数中,我添加了一个clearTimeout和setTimeout为100ms,然后对大小的变化做出反应,以防止在通过拖动角落调整大小时减速。

Assuming I understood your question, this code should work: 假设我理解你的问题,这段代码应该有效:

$(document).ready(function() {

    $(window).on('resize', function() {
        $('.equalize').css('height', highest($('p')));
    }).trigger('resize');

});

function highest(el) {
    var highest = 0;
    el.each(function() {
        var height = $(this).height();
        if(height > highest) {
          highest = height;
        }
    });
    return highest;   
}

Fiddle 小提琴

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

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