简体   繁体   English

等高列表项

[英]Equal Heights List Items

I have a challenge with setting equal heights on some list items on my site. 在网站上的某些列表项上设置相等的高度时,我遇到了挑战。 It works "most" of the time however occasionally it returns the wrong height and there are collisions in content. 它在“大多数”时间都起作用,但是偶尔返回错误的高度,并且内容存在冲突。 The intent is for a button and some text to be positioned absolute to the bottom to align with other list items horizontally. 目的是为了使按钮和某些文本绝对位于底部,以与其他列表项水平对齐。 I'm using jQuery and here is my code: 我正在使用jQuery,这是我的代码:

    setGridItemsEqualHeight();
    function setGridItemsEqualHeight() {
        var $object = jQuery(".grid-style").find('.actions');
        if ($object) {
            var max = 0;
                jQuery(".item-area").each(function(){
                    var h = jQuery(this).height();
                    max = Math.max(max, h);
            });

        jQuery(".item-area").css({ height: max});
        jQuery(".item .details-area .actions").attr("style", "position: absolute; bottom: 0px; left: 50%; margin-right: -50%; transform: translate(-50%, 0%); -webkit-transform: translate(-50%, 0%); -ms-transform: translate(-50%, 0%)"); 
        }
    }

And here is the HTML: 这是HTML:

<li class="item">
  <div class="item-area grid-area">
    <p>Some content here that sits above</p>
    <div class="actions grid">Bottom aligned content positioned content here </div>
  </div>
</li>

Any ideas, suggestions? 有什么想法,建议吗? Thanks. 谢谢。

I have the following function: 我有以下功能:

$.fn.equalizeHeights = function () {
    return this.height(Math.max.apply(this, $(this).map(function (i, e) { return $(e).height() }).get()))
}

Html: HTML:

<div class="parentDiv">
   <div class="item sameHeight" id="item1">
   </div>
   <div class="item sameHeight" id="item2">
   </div>
</div>

Call: 呼叫:

$(".sameHeight").equalizeHeights();

JSFiddle Example JSFiddle示例

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

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