LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

纯 CSS 实现带连接线的树形组件(div版)

freeflydom
2025年9月10日 11:59 本文热度 215

看到XboxYan写的《纯 CSS 实现带连接线的树形组件》,随手改了个div版本,这下没有details和summary的也能用了。

效果一样: 

 

css:

<style>
    .treeview div.summary{
            outline: 0;
            padding-left: 30px;
            background: repeating-linear-gradient( 90deg, #999 0 1px,transparent 0px 2px) 0px 50%/20px 1px no-repeat;
    }
    .treeview div.details>div.details:last-child{
            background-size: 1px 23px;
    }
    .treeview>div.details:not(:last-child)>div.details:last-child{
            background-size: 1px 100%;
    }
    .treeview div.details{
            padding-left: 38px;
            background: repeating-linear-gradient( #999 0 1px,transparent 0px 2px) 38px 0px/1px 100% no-repeat;
    }
    .treeview div.details>div.details{
            display: none;
            transition: .2s;
    }
    .treeview div.open>div.details{
            display: block;
            transition: .2s;
    }
    .treeview>div.details{
            background: none;
            padding-left: 0;
    }
    .treeview>div.details>div.summary{
            background: none;
            padding-left: 0;
    }
    .treeview>div.details>div.details{
            padding-left: 8px;
            background: repeating-linear-gradient( #999 0 1px,transparent 0px 2px) 8px 0px/1px 100% no-repeat;
    }
    .treeview div.summary{
            display: flex;
            align-items: center;
            height: 46px;
            font-size: 15px;
            line-height: 22px;
            color: rgba(0, 0, 0, 0.85);
            cursor: default;
    }
    .treeview div.summary::after{
            content: '';
            position: absolute;
            left: 10px;
            right: 10px;
            height: 38px;
            background: #EEF2FF;
            border-radius: 8px;
            z-index: -1;
            opacity: 0;
            transition: .2s;
    }
    .treeview div.summary:hover::after{
            opacity: 1;
    }
    .treeview div.summary:not(:only-child)::before{
            content: '';
            width: 14px;
            height: 14px;
            flex-shrink: 0;
            margin-right: 8px;
            border: 1px solid #999;
            background: linear-gradient(#999, #999) 50%/1px 10px no-repeat,linear-gradient(#999, #999)  50%/10px 1px no-repeat;
    }
    .treeview div.open>div.summary::before{
            background: linear-gradient(#999, #999) 50%/10px 1px no-repeat;
    }
</style>

html:

<div class="treeview" id="treeview">
        <div class="details">
                <div class="summary tree-item">项目1</div>
                <div class="details">
                        <div class="summary tree-item">文件夹</div>
                        <div class="details">
                                <div class="summary tree-item">sdd</div>
                        </div>
                </div>
                <div class="details">
                        <div class="summary tree-item">chrome test</div>
                </div>
        </div>
        <div class="details">
                <div class="summary tree-item">项目2</div>
                <div class="details">
                        <div class="summary tree-item">文件夹2-1</div>
                        <div class="details">
                                <div class="summary tree-item">文件夹2-1-1</div>
                                <div class="details">
                                        <div class="summary tree-item">文件夹2-1-1-1</div>
                                        <div class="details">
                                                <div class="summary tree-item">文件夹2-1-1-1-1</div>
                                                <div class="details">
                                                        <div class="summary tree-item">文件夹2-1-1-1-1-1</div>
                                                </div>
                                        </div>
                                </div>
                        </div>
                </div>
        </div>
        <div class="details">
                <div class="summary tree-item">文件夹1</div>
                <div class="details">
                        <div class="summary tree-item">文件夹</div>
                </div>
                <div class="details">
                        <div class="summary tree-item">文件夹2</div>
                </div>
        </div>
        <div class="details">
                <div class="summary tree-item">项目3</div>
                <div class="details">
                        <div class="summary tree-item">文件夹1</div>
                        <div class="details">
                                <div class="summary tree-item">1</div>
                                <div class="details">
                                        <div class="summary tree-item">文件夹2</div>
                                </div>
                        </div>
                </div>
                <div class="details">
                        <div class="summary tree-item">文件夹</div>
                </div>
                <div class="details">
                        <div class="summary tree-item">文件夹</div>
                </div>
                <div class="details">
                        <div class="summary tree-item">文件夹</div>
                </div>
                <div class="details">
                        <div class="summary tree-item">文件夹的副本</div>
                </div>
                <div class="details">
                        <div class="summary tree-item">点点点</div>
                </div>
                <div class="details">
                        <div class="summary tree-item">hewei</div>
                        <div class="details">
                                <div class="summary tree-item">hewei02</div>
                        </div>
                </div>
        </div>
</div>

script:

<script>
        document.addEventListener('DOMContentLoaded', function() {
                document.querySelectorAll('#treeview div.summary').forEach(summary => {
                        summary.addEventListener('click', (e)=>{
                                if (e.currentTarget.nextElementSibling !== null)
                                        e.currentTarget.parentElement.classList.toggle('open');
                        });
                });
        });
</script>

如需兼容更低版本,jQuery版实现如下(代码貌似更简洁):

<script>
    $(document).ready(function() {
        $('#treeview div.summary').click(function(e) {
            if ($(this).next().length > 0) {
                $(this).parent().toggleClass('open');
            }
        });
    });
</script>



该文章在 2025/9/10 12:02:07 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2025 ClickSun All Rights Reserved