0
点赞
收藏
分享

微信扫一扫

STM32学习笔记-GPIO

ITWYY 2024-11-06 阅读 33

适用场景 省市县多级分类,文件夹目录,族谱瓜瓞图,多级分类的展现。

经典代码:5KB实现html+js+css+json无限极分类展现带连线完整实例


<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>无限级树形图</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            background-color: #f0f0f0;
            height: 100vh;
            margin: 0;
        }

        .tree-container {
            width: 88.88%;
            padding: 20px;
            background-color: #fff;
            border: 1px solid #ddd;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            overflow-y: auto;
            height: 88vh;
        }

        .node {
            list-style-type: none;
            padding: 2px 8px;
            position: relative;
        }

        /* 连接线样式 */
        .node:before {
            content: '';
            position: absolute;
            top: 12px;
            left: -18px;
            width: 25px;
            height: 1px;
            background-color: #333;
        }

        .children {
            display: block; /* 默认展开 */
            padding-left: 28px;
            position: relative;
        }

        /* 同级连接线 */
        .children:before {
            content: '';
            position: absolute;
            top: 0;
            bottom: 0;
            left: 10px;
            width: 1px;
            background-color: #333;
        }

        /* 树节点样式 */
        .node-toggle {
            cursor: pointer;
            font-weight: bold;
            color: #007BFF;
        }
    </style>
</head>
<body>

<div class="tree-container" id="treeContainer"></div>

<script>
    // 示例 JSON 数据
    const locationData = [
        {
            name: "亚洲",
            children: [
                {
                    name: "中国",
                    children: [
                        {
                            name: "北京市",
                            children: [
                                { name: "东城区" },
                                { name: "西城区" },
                                { name: "朝阳区" }
                            ]
                        },
                        {
                            name: "广东省",
                            children: [
                                {
                                    name: "广州市",
                                    children: [
                                        { name: "天河区" },
                                        { name: "越秀区" }
                                    ]
                                },
                                {
                                    name: "深圳市",
                                    children: [
                                        { name: "南山区" },
                                        { name: "福田区" }
                                    ]
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    ];

    // 创建树节点的递归函数
    function createTreeNode(data) {
        const nodeElement = document.createElement("li");
        nodeElement.classList.add("node", "expanded");

        // 创建节点名称元素
        const nodeLabel = document.createElement("span");
        nodeLabel.classList.add("node-toggle");
        nodeLabel.innerText = data.name;

        // 如果有子节点,添加子节点容器并递归创建子节点
        if (data.children && data.children.length > 0) {
            const childrenContainer = document.createElement("ul");
            childrenContainer.classList.add("children");

            // 递归生成子节点
            data.children.forEach(child => {
                const childNode = createTreeNode(child);
                childrenContainer.appendChild(childNode);
            });

            nodeElement.appendChild(nodeLabel);
            nodeElement.appendChild(childrenContainer);
        } else {
            // 如果没有子节点,直接添加标签
            nodeElement.appendChild(nodeLabel);
        }

        return nodeElement;
    }

    // 渲染树结构
    function renderTree(data, container) {
        const treeRoot = document.createElement("ul");
        data.forEach(item => {
            const treeNode = createTreeNode(item);
            treeRoot.appendChild(treeNode);
        });
        container.appendChild(treeRoot);
    }

    // 获取树容器
    const treeContainer = document.getElementById("treeContainer");

    // 初始化树
    renderTree(locationData, treeContainer);
</script>

</body>
</html>
举报

相关推荐

0 条评论