gpt4 book ai didi

javascript - 具有固定高度和水平滚动的 HTML 列布局

转载 作者:太空宇宙 更新时间:2023-11-04 14:49:45 27 4
gpt4 key购买 nike

对于 android 元素,我需要显示一个动态加载内容的 View (WebView)。内容项将为 <div class="content-item"> JavaScript 添加的标签至 <div class="content-holder">页面加载后。

设计是这样的:

  • 列表项
  • 内容项将以固定宽度的列进行布局。内容项不一定具有相同的高度。
  • 任何列的高度都不得超过屏幕高度。如果一列已满,额外的内容将被放入下一列(必要时创建列)。不会有空列。列不得在内容项内中断。
  • 页面应水平滚动(许多固定宽度的列),但不能垂直滚动(高度不超过页面)。

关于如何使用 css、javascript 实现的任何想法?

最佳答案

http://jsfiddle.net/B4RPJ/

您可以遍历内容项,检查它们是否适合该列,如果不适合,则创建一个新列并将其余元素移动到新列......像这样:

$(".content-item").each( function() {
// iterate the content items and see if the bottom is out of the container
var bottom = $(this).position().top + $(this).height();
if ( bottom > $(this).parent().height() ) {
// shift it and following content items to new column
columnShift( $(this) );
}
} );

function columnShift( el ) {
var parent = el.parent();
var container = $(".content-container");

// create a new column
var newCol = $("<div class='content-holder'></div>");

// get the content items that don't fit and move them to new column
el.nextAll(".content-item").appendTo( newCol );
el.prependTo( newCol );

// widen the parent container
container.width( container.width() + parent.width() );

// add the new column to the DOM
parent.after( newCol );
}

用 html

<div class="content-container">
<div class="content-holder">
<div class="content-item"></div>
<div class="content-item"></div>
<div class="content-item"></div>
<div class="content-item"></div>
<div class="content-item"></div>
</div>
</div>

具有任意数量的 .content-item div,包含任意数量的内容

和CSS

.content-holder {
float: left;
width: 300px;
height: 300px;
border: 1px solid #000000;
overflow: hidden;
margin: 5px;
padding: 10px;
}

.content-item {
max-height: 280px;
overflow: hidden;
}

我相信你可以让代码更聪明,但这应该是一个开始

关于javascript - 具有固定高度和水平滚动的 HTML 列布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17518372/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com