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

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

css:

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:

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:(如需兼容更低版本,可以用jQuery实现)

js 复制代码
<script>
        document.addEventListener('DOMContentLoaded', function() {
                let summaries = document.querySelectorAll('#treeview div.summary');
                summaries.forEach(summary => {
                        summary.addEventListener('click', (e)=>{
                                if (e.currentTarget.nextElementSibling !== null)
                                        e.currentTarget.parentElement.classList.toggle('open');
                        });
                });
        });
</script>
相关推荐
OpenTiny社区11 分钟前
一文解读“Performance面板”前端性能优化工具基础用法!
前端·性能优化·opentiny
拾光拾趣录32 分钟前
🔥FormData+Ajax组合拳,居然现在还用这种原始方式?💥
前端·面试
不会笑的卡哇伊42 分钟前
新手必看!帮你踩坑h5的微信生态~
前端·javascript
bysking43 分钟前
【28 - 记住上一个页面tab】实现一个记住用户上次点击的tab,上次搜索过的数据 bysking
前端·javascript
Dream耀1 小时前
跨域问题解析:从同源策略到JSONP与CORS
前端·javascript
前端布鲁伊1 小时前
【前端高频面试题】面试官: localhost 和 127.0.0.1有什么区别
前端
HANK1 小时前
Electron + Vue3 桌面应用开发实战指南
前端·vue.js
極光未晚1 小时前
Vue 前端高效分包指南:从 “卡成 PPT” 到 “丝滑如德芙” 的蜕变
前端·vue.js·性能优化
郝亚军1 小时前
炫酷圆形按钮调色器
前端·javascript·css
Spider_Man1 小时前
别再用Express了!用Node.js原生HTTP模块装逼的正确姿势
前端·http·node.js