【发布时间】:2014-11-30 12:09:01
【问题描述】:
(HTML和CSS代码在文章底部提供)
我正在使用 Bootstrap 3.0.2 制作一个 Web 应用程序,它帮助我使其大部分都具有响应性。
我想添加一个组织结构图来显示元素的层次结构,所以我找到了一些使用无序列表来显示它的代码。不过,作为初学者,我的问题是让组织结构图符合我的目标,即让网站在大多数屏幕尺寸下看起来都不错。
从下图可以看出,问题在于,当第 2 级的列表项获得太多子项(在第 3 级)时,它会变得太宽并将其兄弟姐妹向下推到下一行。 这样边框(使用伪元素 ::before 和 ::after 制作)将无法正确显示。
我怀疑一旦屏幕尺寸变得太小,解决方案可能涉及重新调整第 3 级及以下每个元素 (-tag) 的大小。问题是我不知道只针对第 3 级标签,因为我希望顶级元素保持相同的大小。也只是想要我可以尝试的方法的一般提示/建议。我愿意更改所有内容,或免费使用任何现有的响应式组织结构图用于商业用途。也许甚至有一个我可以合并的解决方案 将引导元素添加到组织结构图中以满足我的要求?
珍惜任何时间和精力。即使只是一些提示可以将我推向正确的方向也会有很大帮助!
HTML 代码:
<!--
ORG CHART
=========================================-->
<div class="container-fluid" style="margin-top:20px">
<div class="row">
<div class="col-md-12">
<div class="tree">
<ul>
<li>
<a href="#">
<div class="container-fluid">
<div class="row">
Top level
</div>
<div class="row" style="margin-top: 35px;">
<i class="fa fa-exclamation-circle fa-2x"></i>
</div>
<div class="row">
15 Failed Tests
</div>
</div>
</a>
<ul>
<li>
<a href="#">
<div class="container-fluid">
<div class="row">
Customer
</div>
<div class="row" style="margin-top: 35px;">
<i class="fa fa-exclamation-circle fa-2x"></i>
</div>
<div class="row">
3 Failed Tests
</div>
</div>
</a>
</li>
<li>
<a href="#">
<div class="container-fluid">
<div class="row">
Payments
</div>
<div class="row" style="margin-top: 35px;">
<i class="fa fa-exclamation-circle fa-2x"></i>
</div>
<div class="row">
5 Failed Tests
</div>
</div>
</a>
<ul>
<li>
<a href="#">
<div class="container-fluid">
<div class="row">
Send Money
</div>
<div class="row" style="margin-top: 35px;">
<i class="fa fa-exclamation-circle fa-2x"></i>
</div>
<div class="row">
3 Failed Tests
</div>
</div>
</a>
</li>
<li>
<a href="#">
<div class="container-fluid">
<div class="row">
Send Request
</div>
<div class="row" style="margin-top: 35px;">
<i class="fa fa-exclamation-circle fa-2x"></i>
</div>
<div class="row">
2 Failed Tests
</div>
</div>
</a>
</li>
</ul>
</li>
<li>
<a href="#">
<div class="container-fluid">
<div class="row">
Online
</div>
<div class="row" style="margin-top: 35px;">
<i class="fa fa-exclamation-circle fa-inv fa-2x"></i>
</div>
<div class="row">
7 Failed Tests
</div>
</div>
</a>
</li>
</ul>
</li>
</ul>
</div>
</div>
</div>
</div>
CSS 样式:
/*#region Organizational Chart*/
.tree * {
margin: 0; padding: 0;
}
.tree ul {
padding-top: 20px; position: relative;
-transition: all 0.5s;
-webkit-transition: all 0.5s;
-moz-transition: all 0.5s;
}
.tree li {
float: left; text-align: center;
list-style-type: none;
position: relative;
padding: 20px 5px 0 5px;
-transition: all 0.5s;
-webkit-transition: all 0.5s;
-moz-transition: all 0.5s;
}
/*We will use ::before and ::after to draw the connectors*/
.tree li::before, .tree li::after{
content: '';
position: absolute; top: 0; right: 50%;
border-top: 2px solid #696969;
width: 50%; height: 20px;
}
.tree li::after{
right: auto; left: 50%;
border-left: 2px solid #696969;
}
/*We need to remove left-right connectors from elements without
any siblings*/
.tree li:only-child::after, .tree li:only-child::before {
display: none;
}
/*Remove space from the top of single children*/
.tree li:only-child{ padding-top: 0;}
/*Remove left connector from first child and
right connector from last child*/
.tree li:first-child::before, .tree li:last-child::after{
border: 0 none;
}
/*Adding back the vertical connector to the last nodes*/
.tree li:last-child::before{
border-right: 2px solid #696969;
border-radius: 0 5px 0 0;
-webkit-border-radius: 0 5px 0 0;
-moz-border-radius: 0 5px 0 0;
}
.tree li:first-child::after{
border-radius: 5px 0 0 0;
-webkit-border-radius: 5px 0 0 0;
-moz-border-radius: 5px 0 0 0;
}
/*Time to add downward connectors from parents*/
.tree ul ul::before{
content: '';
position: absolute; top: 0; left: 50%;
border-left: 2px solid #696969;
width: 0; height: 20px;
}
.tree li a{
height: 100px;
width: 200px;
padding: 5px 10px;
text-decoration: none;
background-color: white;
color: #8b8b8b;
font-family: arial, verdana, tahoma;
font-size: 11px;
display: inline-block;
box-shadow: 0 1px 2px rgba(0,0,0,0.1);
-transition: all 0.5s;
-webkit-transition: all 0.5s;
-moz-transition: all 0.5s;
}
/*Time for some hover effects*/
/*We will apply the hover effect the the lineage of the element also*/
.tree li a:hover, .tree li a:hover+ul li a {
background: #cbcbcb; color: #000;
}
/*Connector styles on hover*/
.tree li a:hover+ul li::after,
.tree li a:hover+ul li::before,
.tree li a:hover+ul::before,
.tree li a:hover+ul ul::before{
border-color: #94a0b4;
}
/*#endregion*/
【问题讨论】:
-
快速想法:您是否尝试过使用 % 作为框的宽度?
-
我尝试过这个,但问题是每个级别将包含不同数量的列表项,这也会改变客户端。也许我可以计算每个级别的所有项目,然后为宽度设置一些百分比。
-
但是列表项增加了高度,而不是宽度?如果你担心长文本会搞砸宽度,你总是可以尝试一个 jQuery 插件,让文本适应 div 的宽度(而不是 div 适应文本的宽度):fittextjs.com
标签: javascript jquery html css twitter-bootstrap-3