纯CSS实现树状结构导航

≡放荡痞女 提交于 2020-03-15 08:08:36

在对网站进行mip改造的时候,使用百度的导航组件,很难有较好的展现形式。

百度又不允许自定义JS,所以,考虑增加CSS(是在百度组件的基础上增加JS),用css补充实现点击功能,实现精美的导航。

最终结果是为了实现以下三个样式

1.未展开状态

 

2.展开状态,有二级导航的,要出现+号

 

 

3.点击+号能展开二级菜单

 

 

 参考以下树状CSS的设计思路

<!DOCTYPE HTML>
<html>
<head>
<title>纯CSS可折叠树状菜单</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<style type="text/css">
    body, ul, li{margin: 0;padding: 0;}
    body { background-color:#e4e9f1; color:#002446; margin: 0; }
    input, select, textarea, th, td { font-size: 1em; }
    ol.tree {padding: 0 0 0 30px;width: 300px;}
    li {position: relative;margin-left: -15px;list-style: none;}
    li.file{margin-left: -18px !important;}
    li.file a{background: url(document.png) 0 0 no-repeat;color: #002446;padding-left: 21px;text-decoration:none;display: block;}
    li input{position: absolute;left: 0;margin-left: 0;opacity: 0;z-index: 2;cursor: pointer;height: 1em;width:1em;top: 0;}
    input + ol{display: none;}
    input + ol > li { height: 0; overflow: hidden; margin-left: -14px !important; padding-left: 1px; }
    li label {cursor: pointer;display: block;padding-left: 17px;background: url(toggle-small-expand.png) no-repeat 0px 1px;}
    input:checked + ol {background: url(toggle-small.png) 44px 5px no-repeat;margin: -22px 0 0 -44px;padding:27px 0 0 80px;height: auto;display: block;}
    input:checked + ol > li { height: auto;}

</style>
</head>
<body>
    
    <ol class="tree">
        <li>
            <label for="folder1">水产养殖</label> <input type="checkbox"  id="folder1" checked="checked" /> 
            <ol>

                <li class="file"><a href="#">实时数据</a></li>
                <li>
                    <label for="subfolder1">实时数据</label> <input type="checkbox" id="subfolder1" /> 
                    <ol>
                        <li class="file"><a href="">下级</a></li>
                        <li>
                            <label for="subsubfolder1">下级</label> <input type="checkbox" id="subsubfolder1" /> 
                            <ol>

                                <li class="file"><a href="">下级</a></li>
                                <li>
                                    <label for="subsubfolder2">下级</label> <input type="checkbox" id="subsubfolder2" /> 
                                    <ol>
                                        <li class="file"><a href="">无限级</a></li>
                                        <li class="file"><a href="">无限级</a></li>
                                        <li class="file"><a href="">无限级</a></li>

                                        <li class="file"><a href="">无限级</a></li>
                                        <li class="file"><a href="">无限级</a></li>
                                        <li class="file"><a href="">无限级</a></li>
                                    </ol>
                                </li>
                            </ol>
                        </li>

                        <li class="file"><a href="">下级</a></li>
                        <li class="file"><a href="">下级</a></li>
                        <li class="file"><a href="">下级</a></li>
                        <li class="file"><a href="">下级</a></li>
                    </ol>
                </li>
            </ol>

        </li>
        <li>
            <label for="folder2">水产养殖</label> <input type="checkbox" id="folder2" /> 
            <ol>
                <li class="file"><a href="">实时数据</a></li>
                <li>
                    <label for="subfolder2">实时数据</label> <input type="checkbox" id="subfolder2" /> 
                    <ol>

                        <li class="file"><a href="">下级</a></li>
                        <li class="file"><a href="">下级</a></li>
                        <li class="file"><a href="">下级</a></li>
                        <li class="file"><a href="">下级</a></li>
                        <li class="file"><a href="">下级</a></li>
                        <li class="file"><a href="">下级</a></li>

                    </ol>
                </li>
            </ol>
        </li>
    </ol>
    
</body>
</html>

  最终代码回头贴上

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!