简体   繁体   English

HTML / CSS标签式导航

[英]HTML / CSS Tabbed navigation

I am developing an application where users will be able to open tabs. 我正在开发一个应用程序,用户可以在其中打开标签页。 For your note, I am using the Bootstrap CSS framework, and it has a tab plugin as well. 请注意,我使用的是Bootstrap CSS框架,它也有一个tab插件。 (In case I need a more flexible approach I can use a different library for this job.) (如果我需要更灵活的方法,则可以使用其他库来完成此工作。)

http://twitter.github.com/bootstrap/javascript.html#tabs http://twitter.github.com/bootstrap/javascript.html#tabs

What I need is to have a flexible width for tabs. 我需要的是让标签的宽度灵活。 It should work like Chrome/Firefox's tabs. 它应该像Chrome / Firefox的标签一样工作。 If the collective width of the tabs is less than the width of the bar, it should work as it is is. 如果选项卡的总宽度小于条形图的宽度,则应按原样工作。 However if it is longer than the width of the bar, then the size of each tab width should reduce equally. 但是,如果它长于条的宽度,则每个选项卡宽度的大小应同等减小。 (If you open 10-20 tabs using chrome/firefox, you'll understand what I mean) (如果您使用chrome / firefox打开10-20个标签,您将会理解我的意思)

Is there any easy way to do this, without using javascript? 有没有使用javascript的简便方法? The only solution that comes up to my mind is to get the inner width of the bar using javascript and get the width of the other tabs and sum them up. 我想到的唯一解决方案是使用javascript获取条形图的内部宽度,并获取其他选项卡的宽度并将其汇总。 If the sum is higher, then I can resize each tab. 如果总和更高,则可以调整每个标签的大小。

I can implement this solution but I fear it will be hard to ensure cross browser compatibility. 我可以实现此解决方案,但是我担心很难确保跨浏览器的兼容性。 Is there any easier way to achieve this? 有没有更简单的方法来实现这一目标?

Thanks in advance, 提前致谢,

You could adjust the width of nav nav-tabs li elements after they are rendered. 您可以在呈现nav nav-tabs li元素后调整其宽度。

Calculate how many tabs fit in the parent element at their normal size and reduce each tab by a percentage of a single tab width for each tab that exceeds the max number. 计算按正常大小在父元素中容纳多少个选项卡,并为每个选项卡将超出最大数量的单个选项卡宽度减少一个选项卡宽度的百分比。

You don't need JavaScript as long as you can rely on a CSS algorithm that adapt width to its content: the table layout algorithm :) 您不需要JavaScript,只要您可以依靠CSS算法来调整宽度以适应其内容即可: 表布局算法:)

See http://jsfiddle.net/r9yrM/1/ from my previous answer for examples. 有关示例,请参见我以前的回答中的 http://jsfiddle.net/r9yrM/1/

Don't forget to have a minimum padding on each "cell", text stuck to a border isn't very readable (and ugly). 不要忘记在每个“单元格”上设置最小填充量,粘贴在边框上的文本不是很可读(而且很难看)。

With JS, you could decide of a maximum number of tabs (or a minimum "reasonable" width) and above (below) that number, add a class on the parent that will trigger each tab to render as float: left and block and not table-cell anymore. 使用JS,您可以决定最大数量的标签页(或最小的“合理”宽度),并在该数目以上(以下)确定,在父级上添加一个类,该类将触发每个标签页呈现为float: leftblock而不是table-cell了。 Then it'll occupy 2 or more lines (like the extension Tab Mix Plus on Firefox) 然后它将占用2行或更多行(例如Firefox上的扩展Tab Mix Plus

I've tried to solve the same problem today... maybe my solution helps you a little more: http://www.da3x.de/blog/flexible-tab-panel-with-overlapping-elements/ 我今天尝试解决相同的问题...也许我的解决方案可以为您提供更多帮助: http : //www.da3x.de/blog/flexible-tab-panel-with-overlapping-elements/

It uses HTML + CSS only... no JS so far. 它仅使用HTML + CSS,到目前为止还没有JS。 But I think I'd need some JS to react on the actual width of the browser window. 但是我认为我需要一些JS才能对浏览器窗口的实际宽度做出反应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM