繁体   English   中英

如何使用CSS,HTML和一些Javascript显示二叉搜索树?

[英]How to display a binary search tree using CSS, HTML and a bit of Javascript?

我想使用CSS和HTML在屏幕上“绘制”树,而不是以任何方式或数据结构表示它...

用css限制二进制和unilevel树

演示: http//jsfiddle.net/Limitlessisa/5Lhb0ron/

HTML:

<div class="tree">

        <ul>
            <li>
                <div><input type="checkbox"> Main <br/> <button> Test Btn </button></div>
                <ul>
                    <li>
                        <div><input type="checkbox"> Sub-1</div>
                    </li>
                    <li>
                        <div><input type="checkbox"> Sub-2</div>
                        <ul>
                            <li>
                                <div><input type="checkbox"> Sub-2-1</div>
                            </li>
                            <li>
                                <div><input type="checkbox"> Sub-2-2</div>
                            </li>
                        </ul>  
                    </li>
                </ul>

            </li>
        </ul>

    </div>

CSS:

<style>
.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;
}

.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;
}

.tree li:only-child::after, .tree li:only-child::before {
    display: none;
}

.tree li:only-child{ padding-top: 0;}

.tree li:first-child::before, .tree li:last-child::after{
    border: 0 none;
}
.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;
}
.tree ul ul::before{
    content: '';
    position: absolute; top: 0; left: 50%;
    border-left: 1px solid #ccc;
    width: 0; height: 20px;
}
.tree li div{
    border: 1px solid #ccc;
    padding: 5px 10px;
    text-decoration: none;
    color: #666;
    font-family: arial, verdana, tahoma;
    font-size: 11px;
    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;
}
.tree li div:hover, .tree li div:hover+ul li div {
    background: #c8e4f8; color: #000; border: 1px solid #94a0b4;
}
.tree li div:hover+ul li::after, 
.tree li div:hover+ul li::before, 
.tree li div:hover+ul::before, 
.tree li div:hover+ul ul::before{
    border-color:  #94a0b4;
}

</style>

使用HTML和CSS创建树状结构的一种非常简单的方法是嵌套<div>
每个div代表一个节点,内部可以有多个节点:

<div> //root
    <div> //child 1
        <div> //child 1.1
            <div></div> //child 1.1.1
            <div></div> //child 1.1.2
            <div></div> //child 1.1.3
        </div>
        <div></div> //child 1.2
    </div>
    <div></div> //child 2
</div>

然后,您可以为所有div添加margin-top ,以便它们显示在其父div之下。

一个JSFiddle: http//jsfiddle.net/VxRmc/

我已经为每个div添加了百分比宽度。 如果知道节点有多少个子节点,则可以计算宽度。 或者你可以使用固定宽度......

是的,它不是一棵美丽的树,但它不能简单。

暂无
暂无

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

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