像 Facebook 时间轴一样,将高度可变的 div 堆叠在 2 列中

发布于 2025-01-02 19:54:44 字数 459 浏览 0 评论 0原文

我想创建自然的内容流。我现在面临的问题是

只会彼此相邻排列。它们不会穿过对面浮动方块的底部边缘。

下图清楚地说明了问题。假设我有 4 个高度可变的

  • Div1 始终从左侧开始
  • Div2 始终显示在右侧
  • Div3 位于左侧或右侧,具体取决于 Div1 的高度和Div2
  • Div4 在这种情况下,Div4 没有粘到 Div2 的底部
  • Div5 也会出现同样的问题

所以,div 的位置 > Div2 应由前面的 div 的高度确定。您能告诉我如何实现这一目标吗?

在此处输入图像描述

I want to create a natural flow of content. The problem that I now face is that the <div>s will only line up next to each other. They will not pass the bottom edge of the floated block on the opposite side.

The following illustration clearly shows the problem. Let's say that I have 4 <div>s with variable heights:

  • Div1 always starts left
  • Div2 always is displayed on the right side
  • Div3 is on the left or right side, depending on the hight of Div1 and Div2
  • Div4 in this situation, Div4 doesn't stick to Div2's bottom
  • Div5 the same problem occurs

So, the position of the divs > Div2 should be determined by the height of the previous divs. Could you please advise me on how to achieve this?

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

幽梦紫曦~ 2025-01-09 19:54:44

检查 Facebook CSS 和 HTML 后,我发现他们使用 list 并在 left 之间交替 li 元素上的 float 来实现此目的right (即每个偶数元素都向右浮动)

例如:

HTML

<ul>
    <li>...</li>
    <li>...</li>
    <li>...</li>
</ul>

CSS

li {
    clear: left;
    float: left;
    display: block;
    height: 100px;
    width: 200px;
    margin-bottom: 10px;
}

li:nth-child(2n) {
    clear: right;
    float: right;
}

工作示例:http://jsfiddle.net/gBVPj/

仅当一侧的元素不超过另一侧的两个元素的高度时,此方法才有效。例如,在您的图表中,如果框 2 的高度大于框 1 和框 3 的总高度,则框 5 将被移位并与框 4 内联定位。

像这样: http://jsfiddle.net/gBVPj/1/

我在 Facebook 上没有找到此问题的示例(一个元素永远不会超过的高度2)所以我相信他们已经考虑到了这一点。他们可能通过使用 JavaScript 来实现这一点 - 如果您检查元素,它们有一个与元素高度匹配的属性 data-height

After checking the Facebook CSS and HTML, I found they achieve this using a list and alternating the float on the li elements between left and right (ie, every even element is floated right)

For example:

HTML

<ul>
    <li>...</li>
    <li>...</li>
    <li>...</li>
</ul>

CSS

li {
    clear: left;
    float: left;
    display: block;
    height: 100px;
    width: 200px;
    margin-bottom: 10px;
}

li:nth-child(2n) {
    clear: right;
    float: right;
}

Working example: http://jsfiddle.net/gBVPj/

This approach only works if an element on one side does not exceed the height of two elements on the other side. For example, in your diagram, should box 2 have a height larger than that of box 1 and 3 combined, then box 5 will be displaced and positioned inline with box 4.

Like this: http://jsfiddle.net/gBVPj/1/

I have not found an example of this problem on Facebook (one element never exceeds the height of two) so I believe that they have taken this into account. They could possibly be achieving this by using JavaScript - if you check the elements, they have a property data-height which matches the height of the element.

甲如呢乙后呢 2025-01-09 19:54:44

我昨晚做了这个工作,发现了一个相当简单的方法。

比较左列和右列的底部位置,将新的 li 元素追加到值较小的一侧,可以通过以下方式完成:

var last_left_post = $('.left:last');
var last_right_post = $('.right:last');
var left_position = 0;
var right_position = 0;

left_position = last_left_post.height() + last_left_post.offset().top;
right_position = last_right_post.height() + last_right_post.offset().top;

if(left_position<=right_position){
    $('#timeline').append('<li class="left"></li>');
}else{
    $('#timeline').append('<li class="right"></li>');
}

.left 和 .right 使用与您相同的 css。

I worked on this last night and found a rather simple way to do.

Compare the bottom position of the left column and the right column, append the new li element to the side with smaller value, which can be done by th e following way:

var last_left_post = $('.left:last');
var last_right_post = $('.right:last');
var left_position = 0;
var right_position = 0;

left_position = last_left_post.height() + last_left_post.offset().top;
right_position = last_right_post.height() + last_right_post.offset().top;

if(left_position<=right_position){
    $('#timeline').append('<li class="left"></li>');
}else{
    $('#timeline').append('<li class="right"></li>');
}

.left and .right using the same css as you do.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文