【发布时间】:2021-02-14 11:03:02
【问题描述】:
我正在画一条连接树节点的线,我或多或少地实现了它,但是我想摆脱一些悬空线等。
这里是 codepen 的实现:- https://codepen.io/Dinesh443/pen/wvWjJeB
我正在尝试摆脱根节点之前的连接线以及最后一个节点之后的其他悬空线,如下所示。
虽然代码是用 Vue js 编写的,但它是一个使用 'ul' 和 'li' 标签的简单递归树表示。我用过::before,::after。伪选择器来实现这一点。
HTML:-
<div class="container">
<h4>Vue.js Expandable Tree Menu<br/><small>(Recursive Components)</small></h4>
<div id="app">
<tree-menu
:nodes="tree.nodes"
:depth="0"
:label="tree.label"
></tree-menu>
</div>
</div>
<script type="text/x-template" id="tree-menu">
<div class="tree-menu">
<li>
<div class="label-wrapper" @click="toggleChildren">
<div :class="labelClasses">
<i v-if="nodes" class="fa" :class="iconClasses"></i>
{{ label }}
</div>
</div>
<ul>
<tree-menu
v-if="showChildren"
v-for="node in nodes"
:nodes="node.nodes"
:label="node.label"
:depth="depth + 1"
>
</ul>
</tree-menu>
</li>
</div>
</script>
CSS:
body {
font-family: "Open Sans", sans-serif;
font-size: 18px;
font-weight: 300;
line-height: 1em;
}
.container {
width: 300px;
margin: 0 auto;
}
.tree-menu {
.label-wrapper {
padding-bottom: 10px;
margin-bottom: 10px;
// border-bottom: 1px solid #ccc;
.has-children {
cursor: pointer;
}
}
}
.tree-menu li {
list-style-type: none;
margin:5px;
position: relative;
}
.tree-menu li>ul::before {
content: "";
position: absolute;
top:-7px;
left:-30px;
border-left: 1px solid #ccc;
border-bottom:1px solid #ccc;
border-radius:0 0 0 0px;
// width:20px;
height:100%;
}
.tree-menu li>ul::after {
content:"";
display: block;
position:absolute;
top:8px;
left:-30px;
border-left: 1px solid #ccc;
border-top:1px solid #ccc;
border-radius:0px 0 0 0;
width:20px;
height:100%;
}
JavaScript(Vue):
let tree = {
label: 'root',
nodes: [
{
label: 'item1',
nodes: [
{
label: 'item1.1'
},
{
label: 'item1.2',
nodes: [
{
label: 'item1.2.1'
}
]
}
]
},
{
label: 'item2'
}
]
}
Vue.component('tree-menu', {
template: '#tree-menu',
props: [ 'nodes', 'label', 'depth' ],
data() {
return {
showChildren: false
}
},
computed: {
iconClasses() {
return {
'fa-plus-square-o': !this.showChildren,
'fa-minus-square-o': this.showChildren
}
},
labelClasses() {
return { 'has-children': this.nodes }
},
// indent() {
// return { transform: `translate(${this.depth * 50}px)` }
// }
},
methods: {
toggleChildren() {
this.showChildren = !this.showChildren;
}
}
});
new Vue({
el: '#app',
data: {
tree
}
})
【问题讨论】:
-
我仍然坚持这一点,我能够使用 not:first-child 属性摆脱根节点之前的行,但其余的悬空线仍然可用。跨度>
标签: javascript html css vue.js sass