浮动元素:先填写底线
我正在开发的一个项目使用选项卡式导航。由于选项卡的数量是动态计算的,并且可能达到很高的数量,有时这些选项卡(本质上是带有 float: left;
的
元素样式声明)溢出到下一行,使用 [###]
显示选项卡,最终结果如下所示:[###] [###] [###] [###] [###] [###]
[###] [###]
[Rest of the content..............]
因为顶行的最后 4 个元素没有它们的元素“连接到”,这看起来很糟糕。
是否可以在 Javascript(如 jQuery 或 MooTools 等框架可接受,如果它们提供更短/更简单的解决方案)的帮助下先填写底行,然后将其余元素放在顶部?
像这样:
[###] [###]
[###] [###] [###] [###] [###] [###]
[Rest of the content..............]
(问题标记为 MooTools,因为它是我们当前使用的 JS 框架。在任何其他框架中给出的答案都可以,因为它可能会快速转换为 MT)
One project I'm working on uses tabbed navigation. Because the number of tabs is dynamically calculated, and can reach high numbers, sometimes these tabs (which are, in essence, <li>
elements with a float: left;
style declaration) overflow into the next line, Using [###]
to display a tab, the end result looks something like this:
[###] [###] [###] [###] [###] [###]
[###] [###]
[Rest of the content..............]
Because the last 4 elements on the top row do not have an element they 'connect to' , this just looks horrible.
Is it possible, with the help of Javascript (frameworks such as jQuery or MooTools acceptable, if they provide a shorter/easier solution) to fill out the bottom row first, and placing the remainder of the elements on top?
Like this:
[###] [###]
[###] [###] [###] [###] [###] [###]
[Rest of the content..............]
(Question tagged MooTools, since it's the JS framework we're currently using. An answer given in any other framework would be fine, since it could probably be translated into MT quickly)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
下面是我用来解决这个问题的代码。
基本上,使用 JS,我们计算容器的宽度,并与选项卡的宽度(所有选项卡具有相同的宽度)一起计算每行选项卡的数量,以及余数。使用简单的模比较,我们可以插入
clear: Both
样式(强制选项卡移动到空行)。Below is the code I've used to solve this matter.
Basically, using JS, we calculate the width of the container, and together with the width of the tabs (all tabs have the same width) calculate the number of tabs per row, and the remainder. Using a simple modulo comparison, we can then insert a
clear: both
style (forcing the tab to move to an empty row).恐怕没有简单的解决方案可以完全满足您的要求。花车就是花车。最好的选择是使用 JavaScript 实时计算选项卡的尺寸和数量,然后使用
position:absolute
手动定位它们。不漂亮。但将大量标签安装到狭小的空间中的问题是可以解决的。最简单的方法是为第二行中的选项卡提供一个额外的 css 类,并使用它将它们放置在第一行的顶部。这可以使用 javascript 在服务器端或客户端完成。或者,如果您知道选项卡的确切数量及其容器的确切宽度,那么您可以为它们设置 max-width 并让它们缩小一点。或者你可以使用
overflow:scroll
作为选项卡容器,但这肯定看起来很难看:)I'm afraid that there is no simple solution to do exactly what you want. Floats are floats. Your best bet will be counting the dimensions and number of tabs on-the-fly with javascript, then positioning them manually with
position:absolute
. Not pretty.But the problem of fitting lots of tabs into small space is solvable. The simplest method is to give the tabs in the second row an additional css class and use it to position them on top of the first row. This can be done on server side or on client side, with javascript. Or, if you know the exact number of tabs and exact width of their container, then you can set
max-width
for them and let them shrink a little. Or you can go withoverflow: scroll
for tabs container, but this will sure look ugly :)用 JS 就可以轻松实现。小于伪代码:
div
(可选)ul
li
填充它,直到达到给定的最大数量或他们的
组合宽度“略低于”所需的宽度
width
div
添加到您的 html 文档中,保存对 id 重复的引用
到保存的 div,在前面添加新的
那。
Easy with JS. Less-than-pseudo code:
div
(optionally)ul
li
s until either thegiven max number is reached or their
combined width is "just below" the needed
width
div
to your html document,saving a reference to id
to a saved div, add the new one before
that.
我建议将所有选项卡保留在一行上(使其宽度根据需要而定)并滚动到当前选项卡。 (您还需要一个列出所有可用选项卡的下拉菜单,以供导航。)
您尝试做的问题是,如果您确实得到了您想要的东西,选择顶行中的选项卡时会发生什么?
I would suggest keeping all the tabs on one line (make it as wide as required) and scrolling to the current tab. (You'll want a drop-down menu listing all of the available tabs as well, for navigation.)
The problem with what you're trying to do is that if you do get what you want, what happens when a tab in the top row is selected?