浮动元素:先填写底线

发布于 2024-08-22 17:44:56 字数 637 浏览 10 评论 0原文

我正在开发的一个项目使用选项卡式导航。由于选项卡的数量是动态计算的,并且可能达到很高的数量,有时这些选项卡(本质上是带有 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 技术交流群。

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

    发布评论

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

    评论(4

    叹沉浮 2024-08-29 17:44:56

    下面是我用来解决这个问题的代码。
    基本上,使用 JS,我们计算容器的宽度,并与选项卡的宽度(所有选项卡具有相同的宽度)一起计算每行选项卡的数量,以及余数。使用简单的模比较,我们可以插入 clear: Both 样式(强制选项卡移动到空行)。

    var allTabs = $('#tab-container li');
    var tabNum = allTabs.length;
    var tabWidth = allTabs[0].offsetWidth;
    var oWidth = $('tab-container').offsetWidth;
    var tabRowNum = Math.floor(oWidth/tabWidth);
    var tabRowOffset = (tabNum % tabRowNum);
    
    for(var i = 0; i < tabNum; i++) {
        if((i % tabRowNum) == tabRowOffset) {
            allTabs[i].setStyle('clear', 'both');
        }
        else {
            allTabs[i].setStyle('clear', 'none');
        }
    }
    

    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).

    var allTabs = $('#tab-container li');
    var tabNum = allTabs.length;
    var tabWidth = allTabs[0].offsetWidth;
    var oWidth = $('tab-container').offsetWidth;
    var tabRowNum = Math.floor(oWidth/tabWidth);
    var tabRowOffset = (tabNum % tabRowNum);
    
    for(var i = 0; i < tabNum; i++) {
        if((i % tabRowNum) == tabRowOffset) {
            allTabs[i].setStyle('clear', 'both');
        }
        else {
            allTabs[i].setStyle('clear', 'none');
        }
    }
    
    瀟灑尐姊 2024-08-29 17:44:56

    恐怕没有简单的解决方案可以完全满足您的要求。花车就是花车。最好的选择是使用 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 with overflow: scroll for tabs container, but this will sure look ugly :)

    弥枳 2024-08-29 17:44:56

    用 JS 就可以轻松实现。小于伪代码:

    • 创建一个 div (可选)
    • 创建一个 ul
    • li 填充它,直到
      达到给定的最大数量或他们的
      组合宽度“略低于”所需的宽度
      width
    • div 添加到您的 html 文档中,
      保存对 id 重复的引用
    • ,但如果您有已保存的引用
      到保存的 div,在前面添加新的
      那。

    Easy with JS. Less-than-pseudo code:

    • Create a div (optionally)
    • Create an ul
    • fill it with lis until either the
      given max number is reached or their
      combined width is "just below" the needed
      width
    • add the div to your html document,
      saving a reference to id
    • repeat, but if you have a saved reference
      to a saved div, add the new one before
      that.
    幼儿园老大 2024-08-29 17:44:56

    我建议将所有选项卡保留在一行上(使其宽度根据需要而定)并滚动到当前选项卡。 (您还需要一个列出所有可用选项卡的下拉菜单,以供导航。)

    您尝试做的问题是,如果您确实得到了您想要的东西,选择顶行中的选项卡时会发生什么?

    • 您有一个选定的选项卡未连接到当前面板(由底行选项卡分隔),这很丑陋,或者
    • 您将顶行选项卡移动到底部(与面板相邻),在这种情况下,您会遇到这种情况您试图避免或
    • 重新组织选项卡以将选定的选项卡放在底部,这会导致导航混乱(用户不可能了解选项卡在哪里)。

    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?

    • You have a selected tab that is not connected to the current panel (separated by the bottom row of tabs), which is ugly or
    • You move the top row of tabs to the bottom (adjacent to the panel) in which case you have the situation you're trying to avoid or
    • You re-organise the tabs to put the selected one on the bottom, which makes a mess of navigation (it is impossible for a user to learn where the tabs are).
    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文