像 Facebook 时间轴一样,将高度可变的 div 堆叠在 2 列中
我想创建自然的内容流。我现在面临的问题是
只会彼此相邻排列。它们不会穿过对面浮动方块的底部边缘。下图清楚地说明了问题。假设我有 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
检查 Facebook CSS 和 HTML 后,我发现他们使用
list
并在left 之间交替
和li
元素上的float
来实现此目的right
(即每个偶数元素都向右浮动)例如:
HTML
CSS
工作示例: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 thefloat
on theli
elements betweenleft
andright
(ie, every even element is floated right)For example:
HTML
CSS
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.我昨晚做了这个工作,发现了一个相当简单的方法。
比较左列和右列的底部位置,将新的 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:
.left and .right using the same css as you do.