如何在 ASP.NET MVC 中创建两列蛇行布局?
我有大约一百个短文本数据项(这个数字可能会有很大差异),我想将它们放入页面中,并让浏览器将其管理到周围 div 内的三列中,并按项目向下然后横向排列,像这样:
A F L
B G M
C H N
D I O
E K ...
有没有办法将其呈现为 li 的(或者可能只是单独的行),并让浏览器自动将其整理成三个等高列,可能使用 CSS?
是否存在浏览器兼容性问题?
I have about a hundred short-text data items (this number can vary substantially) that I would like to put in a page and have the browser manage that into three columns within a surrounding div, arranged with the items going down and then across, like this:
A F L
B G M
C H N
D I O
E K ...
Is there a way to render this out as li's (or maybe just individual lines), and have the browser automatically collate it into three equal-height columns, possibly using CSS?
Are there any browser compatibility issues?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
没有浏览器兼容性问题:
我们使用模运算符来让我们知道除法是否为偶数...如果不是,我们不会为剩余的列添加额外的行。 有关详细信息,请参阅 http://msdn.microsoft.com/en-us /library/0w4e0fzs.aspx
例如,查看 https://stackoverflow.com/users HTML 源 - 它使用
<表>
Without browser compatibility issues:
We use the modulus operator to let us know if the division is even or not... if it is not, we're not going to add an extra row for the leftover columns. For more information, see http://msdn.microsoft.com/en-us/library/0w4e0fzs.aspx
For example look at https://stackoverflow.com/users HTML source - it uses
<table>
当然,在 CSS 世界中,这是我所知道的在表格之外执行三列的唯一方法。
显然,您不会使用所有这些样式标签,而是使用实际的样式。 请注意宽度为 33%。
我在 IE 和 FirFox 中尝试过,都没有问题。
In the CSS world this is the only way I know to do three columns outside of a table of course.
Clearly you wouldn't use all those style tags, instead you'd use an actual style. Notice the 33% as the width.
I tried this in IE and FirFox and no probs on either.
在 Xandy 建议使用 Javascript 后,我在谷歌上搜索了 jQuery 插件,发现了这个:
报纸专栏 JQuery 插件
http://www.webmeisterei.com/petznick /2007/05/16/newspaper-columns-jquery-plugin/
用法:
您需要一个包含诸如...之类的元素的容器
列,
,并且您需要Javascript 中的
只需调用: 使用该插件的网页
http://www.bregenzerwald-news.at/?classifieds=1
After Xandy suggested Javascript, I did some googling for jQuery plugins, and found this:
Newspaper Columns JQuery Plugin
http://www.webmeisterei.com/petznick/2007/05/16/newspaper-columns-jquery-plugin/
Usage:
You need a Container with Elements like
...and you need the Columns
In Javascript just call:
A web page that uses the plugin
http://www.bregenzerwald-news.at/?classifieds=1
然后将它们放入宽度设置正确的 div 中。
Then place these into a div that has its width correctly set.
html 可以是(实际上并不重要):
在 javascript 中(特别是 jquery 中的示例),您必须用 cols 包装它们,以便生成的 html(在操作 javascript 后)将变为:
Jquery to mainpulate 将是:
代码上面并没有太优化(我相信有很多更好的算法可以做到),但想法就在那里,你使用 javascript 将 html 操作成类似上面的内容,方法是将第一个 1/3 放在左列,第二个1/3 到中心,其余的到右边。 最后一项工作是使用 CSS 将它们分成 3 列,我不会在这里介绍,但是那里有大量的教程,例如 这个 就是这些示例之一。
html can be (actually doesnt really matters):
In javascripts (example in jquery in particular), you have to wrap them with cols, so that the resulting html (after manipulation of javascript) will become:
Jquery to mainpulate will be:
The code above isn't too optimised ( I am sure lots of better algorithm can do), but the idea is there, you use javascript to manipulate the html into something like the above one, by putting the first 1/3 to left column, second 1/3 to center, and the rest to right. The last job is use CSS to make them into 3 columns, which I am not going to cover here but there's tons of tutorials out there, for instance, this one is one of those examples.
你不能单独用 CSS 来做到这一点 - 尽管 @griegs 已经展示了如何做到横向然后向下。
如果你总是想要 3 列,这应该很容易,只需一些服务器端代码和 CSS - 基本上对于每个项目,你都写一些类似的内容:
然后在
count/3
和count*2/3< /code> add:
所以你最终会得到类似的结果:
You can't do this in CSS alone - though @griegs has shown how to do across then down.
If you always want 3 columns this should be easy with a little server side code and CSS - basically for each item you write something like:
Then at
count/3
andcount*2/3
add:So you end up with something like: