【问题标题】:equal height layout with scrollable div具有可滚动 div 的等高布局
【发布时间】:2014-07-26 04:22:30
【问题描述】:

我的布局如下:

     <div class="lower-content">
            <div class="col-1">
                <div class="scrollbox">
                    ........
                </div>
            </div>
            <div class="col-2">
                <img src="http://placehold.it/616x697/AA6600" alt="middle image" />
            </div>
            <div class="col-3">
                <p> .....
                </p>
                <img alt="Small Image" src="http://placehold.it/194x219/446600" />
            </div>
      </div> 

我到目前为止的尝试显示在小提琴中

小提琴http://jsfiddle.net/gg47G/2/

为了更好的观看http://jsfiddle.net/gg47G/2/show

问题:我想要的是所有三列的高度相同,并且在屏幕宽度=768px 之前一直响应。我已经弄清楚了手机的布局(低于 768px),只剩下桌面和平板电脑版本。

浏览器:我也需要一个适用于 IE8 的解决方案。

col-3 添加边距和内边距是可以接受的,但高度应与col1col2 匹配 Jquery 解决方案是可以接受的,但没有外部库。虽然我更喜欢 css 解决方案,但我认为这是不可能的。

【问题讨论】:

  • 对于等高的列,只要你的目标是IE9或更高版本,你就可以使用css-only display: table-cell
  • 抱歉忘了说,我也需要IE8。此外,它目前无法正常工作,即使在 chrome 和 firefox 中也是如此
  • 如果你愿意使用jQuery,它会提供最好的跨浏览器兼容性。我会用$({selector}).height() 比较每列的高度。然后将每个元素的高度设置为最高元素的高度。然后您可以在$(window).resize(function(){}) 中重复此功能。
  • 我已经做到了,到目前为止,我已经得到了 col1col2 来匹配高度。问题是关于col3。由于col3的总内容高度较小,所以底部留有空白
  • @Ani - 那么你对 col3 的期望是什么?我原以为底部会有空白。 (当然,如果你给它一个背景颜色,你会看到列高是正确的。)

标签: javascript jquery html css


【解决方案1】:

小提琴:fiddle

这里是js代码

$(document).ready(function(){
    commonHeightToAllDiv('.common');
});
// value will be changed if resied
$(window).resize(function(){
    commonHeightToAllDiv('.common')
});
function commonHeightToAllDiv(handeler){
    // calculating all the heights of .common
    var heights = $(handeler).map(function () {
            return $(this).height();
        }).get();

    // calculating maximum value among the heights
    var max_height_of_content_boxes = function (array)             {
        return Math.max.apply(Math, array);
    };

    // implementing maximum to all .common
    $(handeler).height(max_height_of_content_boxes(heights));
}

CSS 代码

.lower-content {
    border-top: 1px solid #b2b2b2;
    margin: 1% auto 0;
    padding: 1% 0 0;
    width:1200px;
}

.col-1 {
    display: inline-block;
    padding: 0 1% 0 0;
    vertical-align: top;
    width: 22%;
}
.scrollbox{
    font-size: 1.2em;
    overflow: auto;
}
.col-2 {
    display: inline-block;
    margin: 0 1% 0 0;
    vertical-align: top;
    width: 53%;

 }

.col-3 {
    display: inline-block;
    margin: 0;
    vertical-align: top;
    width: 22%;
}

解释:

每个 div 都有其动态高度。首先我们需要找到那些是什么。然后得到最大值。并在每个 div 上实现该值。调用响应式函数使 js 代码响应式。

【讨论】:

  • 欢迎来到 SO。您应该在此处复制相关代码(jQuery)以供简单参考,并解释您所做的工作。此外,小提琴链接应该是一个 link,而不仅仅是一个 url....
  • 感谢@cale_b 的欢迎信。下次我会记住你的建议。
  • @MonzoorTamal 第一个 div 没有滚动条。
  • @Ani 问题:您想从 div 中选择最大高度并将其实现到 div 吗?如果是,这是解决方案。如果没有,那么你能解释一下吗?如果动态高度不是问题,那么您可以为每个 div 实现固定高度。
猜你喜欢
  • 2019-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-20
  • 1970-01-01
  • 2018-05-17
  • 1970-01-01
相关资源
最近更新 更多