源码网,源码论坛,源码之家,商业源码,游戏源码下载,discuz插件,棋牌源码下载,精品源码论坛

 找回密码
 立即注册
楼主: ttx9n

[CSS] 纯CSS实现可折叠树状菜单

[复制链接]

7万

主题

861

回帖

32万

积分

论坛元老

Rank: 8Rank: 8

积分
329525
发表于 2018-4-17 17:57:49 | 显示全部楼层 |阅读模式
这篇文章主要介绍了纯CSS实现可折叠树状菜单,不用js让你体会到css的强大,需要的朋友可以参考下

1:Html代码

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

实现的思路是运用checkbox的checked值来判断下级栏目是否展开,CSS3的选择器中提供了:checked 这个伪类,这个伪类提供我们,当元素拥有checked这个值的时候就执行你的CSS。(很强大是吧。有了CSS3我们会少写很多JS哦!)

<label for="subsubfolder1">下级</label><input id="subsubfolder1" type="checkbox" />

当checkbox的拥有checked值的时候就就让OL现实出来,达到我们想要的功能。

接下来看看CSS代码吧:

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

这段代码是树状菜单的中心:

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

这个是讲当inoput 拥有了checked后它平级的OL拥有的样式。

使用IE9以下浏览就不用看了,请使用非IE浏览器。(想让IE6+浏览器支持也是可以滴,但是需要加JS来模拟css3属性。国外有很多牛人都写了让IE6+浏览器支持部分CSS3的JS,例如PIE)

总结:

总体来说,实现思路很简单,主要是利用CSS3的 checked 伪类来实现OL的隐藏显示。不过遗憾的是IE游览器不支持CSS3,但我们不能因为IE的不支持而放弃对CSS3的研究。在国外CSS3和HTML5都是前端很热门的话题,他们研究的东西远远超过我们,但国内真正去尝试的还是不多,对于一个前端开发人员来说是一件很可悲的事。我认为CSS3应该引起我们的重视,不能让我们输在起跑线。让我们大家一起来推动CSS3的发展吧。

2:实例源代码

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<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>

 3:效果图

 

20180417175611.png

20180417175611.png

回复

使用道具 举报

0

主题

1万

回帖

0

积分

中级会员

Rank: 3Rank: 3

积分
0
发表于 2022-9-1 02:46:57 | 显示全部楼层
2222222222222222
回复 支持 反对

使用道具 举报

2

主题

2万

回帖

347

积分

中级会员

Rank: 3Rank: 3

积分
347
发表于 2022-9-16 08:30:04 | 显示全部楼层
那三门,你们谁寂寞才快乐撒
回复 支持 反对

使用道具 举报

4

主题

1万

回帖

60

积分

注册会员

Rank: 2

积分
60
发表于 2022-11-30 04:09:14 | 显示全部楼层
的谁vdvdsvdsvdsdsv
回复 支持 反对

使用道具 举报

0

主题

2万

回帖

66

积分

注册会员

Rank: 2

积分
66
发表于 2022-12-31 19:16:20 | 显示全部楼层
啦啦啦啦啦啦哈哈哈
回复 支持 反对

使用道具 举报

2

主题

2万

回帖

73

积分

注册会员

Rank: 2

积分
73
发表于 2023-1-1 05:43:36 | 显示全部楼层
很不错的玩意
回复 支持 反对

使用道具 举报

1

主题

2万

回帖

207

积分

中级会员

Rank: 3Rank: 3

积分
207
发表于 2023-2-7 12:25:56 | 显示全部楼层
看看看看
回复 支持 反对

使用道具 举报

5

主题

2万

回帖

69

积分

注册会员

Rank: 2

积分
69
发表于 2023-9-5 03:23:31 | 显示全部楼层
我要金豆金豆金豆
回复 支持 反对

使用道具 举报

0

主题

2万

回帖

0

积分

中级会员

Rank: 3Rank: 3

积分
0
发表于 2024-5-5 08:54:14 | 显示全部楼层
下载来瞧瞧
回复 支持 反对

使用道具 举报

8

主题

2万

回帖

52

积分

注册会员

Rank: 2

积分
52
发表于 2024-7-27 02:06:17 | 显示全部楼层
刷刷刷刷刷刷刷刷刷刷刷刷刷刷刷
回复 支持 反对

使用道具 举报

高级模式
B Color Image Link Quote Code Smilies

本版积分规则

手机版|小黑屋|网站地图|源码论坛 ( 海外版 )

GMT+8, 2025-2-2 22:38 , Processed in 0.079281 second(s), 28 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表