Stack:Angular 4 + HTML + CSS
我想制作层级树结构,父节点有一个箭头朝向孩子(见下图)。
单击父选项卡时会显示子项。我的功能正常,我已经能够实现以下目标:
我使用的css如下:.tree li::before {
border-left: 1px solid #999;
bottom: 50px;
height: 100%;
top: 0;
width: 1px
}
.tree li::after {
border-top: 1px solid #999;
height: 20px;
top: 25px;
width: 25px
}
我想用以下有向箭头替换线条。 如果有人创建了类似的结构,请指导我。
答案 0 :(得分:4)
您可以使用伪元素实现此目的:
.tree__nested-items {
list-style-type: none;
margin: 0;
padding: 0;
padding-left: 70px;
display: flex;
flex-direction: column;
align-items: flex-start;
}
.tree__item {
width: 200px;
height: 30px;
line-height: 30px;
border: 2px solid #dedede;
background-color: #f7f8fa;
border-radius: 7px;
padding: 10px 25px;
margin: 10px;
}
.tree__item--nested {
position: relative;
}
/* line */
.tree__item--nested:before {
content: "";
position: absolute;
bottom: 50%;
/* padding of container - 10px */
left: -60px;
/* padding of container - 10px */
width: 60px;
/* 3 * half height + half of border radius */
height: calc(150% + 7.5px);
border-bottom: inherit;
border-left: inherit;
border-bottom-left-radius: 15px;
/* don't overlap items */
z-index: -1;
}
/* arrow */
.tree__item--nested:after {
content: "";
position: absolute;
/* border-width - 1px */
left: -8px;
bottom: calc(50% - 6px);
border-color: transparent;
border-left-color: inherit;
border-style: inherit;
border-width: 7px;
}
<div class="tree__item">Some title</div>
<ul class="tree__nested-items">
<li class="tree__item tree__item--nested">Some item one</li>
<li class="tree__item tree__item--nested">Some item two</li>
<li class="tree__item tree__item--nested">Third item</li>
</ul>
根据OP的要求,这里的CSS是更复杂的结构。我们在li
使用before
left
边框,after
用于bottom
边框和圆角以及span
before
对于箭头三角形。
$(function() {
$('.tree li:has(ul)').addClass('parent_li').find(' > span').attr('title', 'Collapse this branch');
$('.tree li.parent_li > span').on('click', function(e) {
var children = $(this).parent('li.parent_li').find(' > ul > li');
if (children.is(":visible")) {
children.hide('fast');
$(this).attr('title', 'Expand this branch').find(' > i').addClass('icon-plus-sign').removeClass('icon-minus-sign');
} else {
children.show('fast');
$(this).attr('title', 'Collapse this branch').find(' > i').addClass('icon-minus-sign').removeClass('icon-plus-sign');
}
e.stopPropagation();
});
});
.tree {
min-height: 20px;
padding: 19px;
margin-bottom: 20px;
background-color: #fbfbfb;
border: 1px solid #999;
-webkit-border-radius: 4px;
-moz-border-radius: 4px;
border-radius: 4px;
-webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.05);
-moz-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.05);
box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.05)
}
.tree li {
list-style-type: none;
margin: 0;
padding: 10px 5px 0 5px;
position: relative
}
.tree li::before,
.tree li::after {
content: '';
left: -20px;
position: absolute;
right: auto
}
.tree li::before {
border-left: 1px solid #999;
bottom: 50px;
height: 100%;
top: 0;
}
.tree li::after {
box-sizing: border-box;
border-left: 1px solid #999;
border-bottom: 1px solid #999;
border-bottom-left-radius: 10px;
height: 25px;
top: 0px;
width: 25px;
}
.tree li span {
-moz-border-radius: 5px;
-webkit-border-radius: 5px;
border: 1px solid #999;
border-radius: 5px;
display: inline-block;
padding: 3px 8px;
text-decoration: none;
}
.tree li.parent_li > span {
cursor: pointer;
}
.tree > ul > li::before,
.tree > ul > li::after {
border: 0;
}
.tree li:last-child::before {
height: 14px;
}
.tree li:not(.parent_li) span {
position: relative;
}
.tree li span:before {
content: "";
position: absolute;
/* border-width - 1px */
left: -4px;
bottom: calc(50% - 4px);
border-color: transparent;
border-left-color: #999;
border-style: inherit;
border-width: 4px;
}
/* another styles for parent_li immediate span children */
.tree li.parent_li > span:before {
bottom: initial;
left: 2px;
top: 20.5px;
}
/* hide arrow for immediate children */
.tree > ul > li > span:before {
display: none;
}
.tree li.parent_li > span:hover,
.tree li.parent_li > span:hover + ul li span {
background: #eee;
border: 1px solid #94a0b4;
color: #000;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css" rel="stylesheet" />
<script src="https://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/js/bootstrap.min.js"></script>
<div class="tree well">
<ul>
<li>
<span><i class="icon-folder-open"></i> Parent</span> <a href="">Goes somewhere</a>
<ul>
<li>
<span><i class="icon-minus-sign"></i> Child</span> <a href="">Goes somewhere</a>
<ul>
<li>
<span><i class="icon-leaf"></i> Grand Child</span> <a href="">Goes somewhere</a>
</li>
</ul>
</li>
<li>
<span><i class="icon-minus-sign"></i> Child</span> <a href="">Goes somewhere</a>
<ul>
<li>
<span><i class="icon-leaf"></i> Grand Child</span> <a href="">Goes somewhere</a>
</li>
<li>
<span><i class="icon-minus-sign"></i> Grand Child</span> <a href="">Goes somewhere</a>
<ul>
<li>
<span><i class="icon-minus-sign"></i> Great Grand Child</span> <a href="">Goes somewhere</a>
<ul>
<li>
<span><i class="icon-leaf"></i> Great great Grand Child</span> <a href="">Goes somewhere</a>
</li>
<li>
<span><i class="icon-leaf"></i> Great great Grand Child</span> <a href="">Goes somewhere</a>
</li>
</ul>
</li>
<li>
<span><i class="icon-leaf"></i> Great Grand Child</span> <a href="">Goes somewhere</a>
</li>
<li>
<span><i class="icon-leaf"></i> Great Grand Child</span> <a href="">Goes somewhere</a>
</li>
</ul>
</li>
<li>
<span><i class="icon-leaf"></i> Grand Child</span> <a href="">Goes somewhere</a>
</li>
</ul>
</li>
</ul>
</li>
<li>
<span><i class="icon-folder-open"></i> Parent2</span> <a href="">Goes somewhere</a>
<ul>
<li>
<span><i class="icon-leaf"></i> Child</span> <a href="">Goes somewhere</a>
</li>
</ul>
</li>
</ul>
</div>
<div class="tree">
<ul>
<li>
<span><i class="icon-calendar"></i> 2013, Week 2</span>
<ul>
<li>
<span class="badge badge-success"><i class="icon-minus-sign"></i> Monday, January 7: 8.00 hours</span>
<ul>
<li>
<a href=""><span><i class="icon-time"></i> 8.00</span> – Changed CSS to accomodate...</a>
</li>
</ul>
</li>
<li>
<span class="badge badge-success"><i class="icon-minus-sign"></i> Tuesday, January 8: 8.00 hours</span>
<ul>
<li>
<span><i class="icon-time"></i> 6.00</span> – <a href="">Altered code...</a>
</li>
<li>
<span><i class="icon-time"></i> 2.00</span> – <a href="">Simplified our approach to...</a>
</li>
</ul>
</li>
<li>
<span class="badge badge-warning"><i class="icon-minus-sign"></i> Wednesday, January 9: 6.00 hours</span>
<ul>
<li>
<a href=""><span><i class="icon-time"></i> 3.00</span> – Fixed bug caused by...</a>
</li>
<li>
<a href=""><span><i class="icon-time"></i> 3.00</span> – Comitting latest code to Git...</a>
</li>
</ul>
</li>
<li>
<span class="badge badge-important"><i class="icon-minus-sign"></i> Wednesday, January 9: 4.00 hours</span>
<ul>
<li>
<a href=""><span><i class="icon-time"></i> 2.00</span> – Create component that...</a>
</li>
</ul>
</li>
</ul>
</li>
<li>
<span><i class="icon-calendar"></i> 2013, Week 3</span>
<ul>
<li>
<span class="badge badge-success"><i class="icon-minus-sign"></i> Monday, January 14: 8.00 hours</span>
<ul>
<li>
<span><i class="icon-time"></i> 7.75</span> – <a href="">Writing documentation...</a>
</li>
<li>
<span><i class="icon-time"></i> 0.25</span> – <a href="">Reverting code back to...</a>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
答案 1 :(得分:1)
如果使用纯css,可能就像这样
.arrow {
position: relative;
width: 100px;
height: 40px;
border-bottom: 2px solid #ccc;
border-bottom-left-radius: 20px;
}
.arrow::after {
content: "";
position: absolute;
left: 100%;
bottom: -7px;
transfotm: translateY(-50%);
border: 6px solid transparent;
border-left-color: #ccc;
}
.main-line {
height: 100px;
width: 2px;
background-color: #ccc;
position: absolute;
left: 8px;
top: 0;
}
&#13;
<div class="main-line"></div>
<div class="arrow"></div>
&#13;
但我认为你必须使用svg这个arows。
答案 2 :(得分:1)
.tree__nested-items {
list-style-type: none;
margin: 0;
padding: 0;
padding-left: 70px;
display: flex;
flex-direction: column;
align-items: flex-start;
}
.tree__item {
width: 200px;
height: 30px;
line-height: 30px;
border: 2px solid #dedede;
background-color: #f7f8fa;
border-radius: 7px;
padding: 10px 25px;
margin: 10px;
}
.tree__item--nested {
position: relative;
}
/* line */
.tree__item--nested:before {
content: "";
position: absolute;
bottom: 50%;
/* padding of container - 10px */
left: -60px;
/* padding of container - 10px */
width: 60px;
/* 3 * half height + half of border radius */
height: calc(150% + 7.5px);
border-bottom: inherit;
border-left: inherit;
border-bottom-left-radius: 15px;
/* don't overlap items */
z-index: -1;
}
/* arrow */
.tree__item--nested:after {
content: "";
position: absolute;
/* border-width - 1px */
left: -8px;
bottom: calc(50% - 6px);
border-color: transparent;
border-left-color: inherit;
border-style: inherit;
border-width: 7px;
}
<div class="tree__item">Some title</div>
<ul class="tree__nested-items">
<li class="tree__item tree__item--nested">Some item one</li>
<li class="tree__item tree__item--nested">Some item two</li>
<li class="tree__item tree__item--nested">Third item</li>
</ul>