将节点移到二进制树HTML中没有任何子节点的最后一行(第n行)

问题描述 投票:1回答:1

我尝试根据要求修改树结构,我试图将节点移动到在二进制树HTML中没有任何子节点的最后一行(第n行)。

需要将1002-Test节点移动到树的第n个级别(级别应等于1006-Test)。我已经尝试过margin-bottom:li标签的auto,我能够得到。但是我无法正确获取连接器,因为我正在用数据动态加载树。我尝试了很多事情,但是没有正确获得链接。如何通过节点之间的链接将节点移至底部?

* {margin: 0; padding: 0;}
.tree ul {
    padding-top: 20px; 
    position: relative;
    padding-left: 0px;

    -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;
    display: inline-block;
    white-space: nowrap;
    vertical-align: top;

    -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: 1px solid #ccc;
    width: 50%; height: 20px;
}
.tree li::after{
    right: auto; left: 50%;
    border-left: 1px solid #ccc;
}

/*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;width: 100%}

/*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: 1px solid #ccc;
    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: 1px solid #ccc;
    width: 0; height: 20px;
}

.tree li a{
    border: 1px solid #ccc;
    padding: 5px 10px;
    text-decoration: none;
    color: #666;
    font-family: arial, verdana, tahoma;
    font-size: 12px;
    display: inline-block;

    border-radius: 5px;
    -webkit-border-radius: 5px;
    -moz-border-radius: 5px;

    -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: #c8e4f8; color: #000; border: 1px solid #94a0b4;
}
/*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;
}

.tree li a.red{
    color: red;
}
.tree li a.green{
    color: green;
}

.tree li a.left{
        text-align: left;
    }
.tree li a.right{
    text-align: right;
}
<div class="tree">
    <ul>
        <li>
            <a href="">Admin </a>
            <ul>
                <li>
                    <a href="" class="green">
                        1001 - Main
                    </a>
                    <ul ng-if="node.children" class="ng-scope">
                        <li>
                            <a href="" class="green left">
                                1002 - Test
                            </a>
                           
                        </li>
                        <li>
                            <a href="" class="red right">
                                1003 - Test
                            </a>
                            <ul ng-if="node.children" class="ng-scope">
                                <li>
                                    <a href="" class="red left">
                                        1006 - Test
                                    </a>
                                    <ul ng-if="node.children" class="ng-scope">
                                <li>
                                    <a href="" class="red left">
                                        1008 - Test
                                    </a>
                                </li>
                                <li>
                                    <a href="" class="red right">
                                        1009 - Test
                                    </a>

                                </li>
                            </ul>
                                </li>
                                <li>
                                    <a href="" class="red right">
                                        1007 - Test
                                    </a>

                                </li>
                            </ul>
                        </li>
                    </ul>
                </li>
            </ul>   
        </li>
    </ul>
</div>
html css less
1个回答
0
投票

您只需要添加这两行:

.tree > ul > li > ul > li > ul > li:first-child > a {margin-top: 66px;}
.tree > ul > li > ul > li > ul > li:first-child::after  {height: 86px;}

* {margin: 0; padding: 0;}
.tree ul {
    padding-top: 20px; 
    position: relative;
    padding-left: 0px;

    -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;
    display: inline-block;
    white-space: nowrap;
    vertical-align: top;

    -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: 1px solid #ccc;
    width: 50%; height: 20px;
}
.tree li::after{
    right: auto; left: 50%;
    border-left: 1px solid #ccc;
}

/*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;width: 100%}

/*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: 1px solid #ccc;
    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: 1px solid #ccc;
    width: 0; height: 20px;
}

.tree li a{
    border: 1px solid #ccc;
    padding: 5px 10px;
    text-decoration: none;
    color: #666;
    font-family: arial, verdana, tahoma;
    font-size: 12px;
    display: inline-block;

    border-radius: 5px;
    -webkit-border-radius: 5px;
    -moz-border-radius: 5px;

    -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: #c8e4f8; color: #000; border: 1px solid #94a0b4;
}
/*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;
}

.tree li a.red{
    color: red;
}
.tree li a.green{
    color: green;
}

.tree li a.left{
        text-align: left;
    }
.tree li a.right{
    text-align: right;
}
.tree > ul > li > ul > li > ul > li:first-child > a {margin-top: 66px;}
.tree > ul > li > ul > li > ul > li:first-child::after  {height: 86px;}
<div class="tree">
    <ul>
        <li>
            <a href="">Admin </a>
            <ul>
                <li>
                    <a href="" class="green">
                        1001 - Main
                    </a>
                    <ul ng-if="node.children" class="ng-scope">
                        <li>
                            <a href="" class="green left">
                                1002 - Test
                            </a>
                           
                        </li>
                        <li>
                            <a href="" class="red right">
                                1003 - Test
                            </a>
                            <ul ng-if="node.children" class="ng-scope">
                                <li>
                                    <a href="" class="red left">
                                        1006 - Test
                                    </a>
                                </li>
                                <li>
                                    <a href="" class="red right">
                                        1007 - Test
                                    </a>

                                </li>
                            </ul>
                        </li>
                    </ul>
                </li>
            </ul>   
        </li>
    </ul>
</div>
© www.soinside.com 2019 - 2024. All rights reserved.