純css實(shí)現(xiàn)多級(jí)折疊菜單折疊樹(shù)效果

1、運(yùn)用checkbox的checked值來(lái)判斷下級(jí)欄目是否展開(kāi),CSS3的選擇器中提供了:checked 這個(gè)偽類,這個(gè)偽類提供我們,當(dāng)元素?fù)碛衏hecked這個(gè)值的時(shí)候就執(zhí)行你的CSS。
當(dāng)有子菜單時(shí),菜單項(xiàng)右側(cè)有向下的箭頭,當(dāng)收起菜單項(xiàng)時(shí),箭頭朝上。圖片可以自己替換。
2、效果圖
3、代碼片段
<ol class="tree"> <li> <label for="folder1" class="folderOne">澤元框架</label> <input type="checkbox" id="folder1" /> <ol> <li> <label for="subfolder1"class="folderTwo">開(kāi)發(fā)規(guī)范</label> <input type="checkbox" id="subfolder1" /> <ol> <li class="file folderThree"><a href="#">常見(jiàn)界面錯(cuò)誤舉例</a></li> <li class="file folderThree"><a href="#">關(guān)于發(fā)行報(bào)告對(duì)BUG管理提出…</a></li> <li class="file folderThree"><a href="#">插件內(nèi)部JAVA包命名規(guī)范</a></li> </ol> </li> <li class="file folderTwo"><a href="#">概述</a></li> <li class="file folderTwo"><a href="#">服務(wù)器集群</a></li> <li class="file folderTwo"><a href="#">模板</a></li> <li class="file folderTwo"><a href="#">安全機(jī)制</a></li> </ol> </li> <li> <label for="folder2" class="folderOne" >ZCMS</label> <input type="checkbox" id="folder2" /> <ol> <li class="file folderTwo"><a href="#">實(shí)時(shí)數(shù)據(jù)</a></li> <li> <label for="subfolder2" class="folderTwo">實(shí)時(shí)數(shù)據(jù)</label> <input type="checkbox" id="subfolder2" /> <ol> <li class="file folderThree"><a href="#">實(shí)時(shí)數(shù)據(jù)</a></li> <li class="file folderThree"><a href="#">實(shí)時(shí)數(shù)據(jù)</a></li> <li class="file folderThree"><a href="#">實(shí)時(shí)數(shù)據(jù)</a></li> </ol> </li> </ol> </li> <li> <label for="folder3" class="folderOne">ZAS</label> <input type="checkbox" id="folder3" /> <ol> <li class="file folderTwo"><a href="#">實(shí)時(shí)數(shù)據(jù)</a></li> <li class="file folderTwo"><a href="#">實(shí)時(shí)數(shù)據(jù)</a></li> </ol> </li> <li> <label for="folder4" class="folderOne">ZHTML標(biāo)簽</label> <input type="checkbox" id="folder4"/> <ol> <li class="file folderTwo"><a href="#">實(shí)時(shí)數(shù)據(jù)</a></li> <li class="file folderTwo"><a href="#">實(shí)時(shí)數(shù)據(jù)</a></li> </ol> </li> <li> <label for="folder5" class="folderOne">UI框架API手冊(cè)</label> <input type="checkbox" id="folder5"/> <ol> <li class="file folderTwo"><a href="#">實(shí)時(shí)數(shù)據(jù)</a></li> <li class="file folderTwo"><a href="#">實(shí)時(shí)數(shù)據(jù)</a></li> </ol> </li> </ol> <style type="text/css"> .tree {margin: 0;padding: 0;background-color:#f2f2f2;overflow: hidden;} /*隱藏input*/ .tree li input{position: absolute;left: 0;opacity: 0;z-index: 2;cursor: pointer;height: 1em;width:1em;top: 0;} /*所有菜單項(xiàng)設(shè)置統(tǒng)一樣式*/ .tree li {position: relative;list-style: none;} /*一級(jí)菜單加下邊線*/ .tree>li{border-bottom: 1px solid #d9d9d9;} /*給有子菜單的菜單項(xiàng)添加背景圖標(biāo)*/ .tree li label {max-width:999px;cursor: pointer;display: block;margin:0 0 0 -50px;padding: 15px 10px 15px 70px;background: url(../../images/cp-detail-arrow-b.png) no-repeat right center;background-position:95% 50%;white-space:nowrap;overflow:hidden;text-overflow: ellipsis; } .tree li label:hover,li label:focus{background-color:#a7a7a7;color:#fff;} /*清除所有展開(kāi)的子菜單的display*/ .tree li input + ol{display: none;} /*當(dāng)input被選中時(shí),給所有展開(kāi)的子菜單設(shè)置樣式*/ .tree input:checked + ol {padding-left:14px;height: auto;display: block;} .tree input:checked + ol > li { height: auto;} /*末層菜單為A標(biāo)簽,設(shè)置樣式*/ .tree li.file a{margin:0 -10px 0 -50px;padding: 15px 20px 15px 70px;text-decoration:none;display: block;color:#333333;white-space:nowrap;overflow:hidden;text-overflow: ellipsis;} .tree li.file a:hover,li.file a:focus{background-color:#a7a7a7;color:#fff;} /*不同層級(jí)的菜單字體大小不同*/ .tree .folderOne{font-size: 18px;} .tree .folderTwo{font-size:16px;} .tree .folderThree{font-size:14px;} </style> <script type="text/javascript"> $(document).ready(function() { //每個(gè)有子菜單的菜單項(xiàng)添加點(diǎn)擊事件 $(".tree label").click(function(){ //獲取當(dāng)前菜單旁邊input的check狀態(tài) var isChecked = $(this).next("input[type='checkbox']").is(':checked'); //展開(kāi)和收齊的不同狀態(tài)下更換右側(cè)小圖標(biāo) if(isChecked){ $(this).css( "background-image","url(../images/cp-detail-arrow-b.png)" ); }else{ $(this).css( "background-image","url(../images/cp-detail-arrow-t.png)" ); } }); }); </script>
下面介紹下CSS 菜單折疊
先給大家展示效果圖:
如上,假設(shè)一級(jí)菜單是 div,二級(jí)菜單是 ul。形如:
<div>業(yè)務(wù)管理<span></span></div> <ul> ... <ul> <div class="collapsed">匯總分析<span></span></</div> <ul> ... <ul> <div class="collapsed">>系統(tǒng)管理<span></span></</div> <ul> ... <ul>
當(dāng)菜單展開(kāi)時(shí),左邊有一個(gè)藍(lán)色的標(biāo)記,右邊是折疊標(biāo)記。
左邊藍(lán)色標(biāo)記自不用表,CSS 設(shè)置 border 即可,右邊利用 CSS 也超方便。
div span { float:right; padding-right:20px; } div span:after { content: "∨" } div.collapsed span:after { content: "∧" }
然后再說(shuō)子菜單的折疊效果,有動(dòng)畫噢:
div.collapsed + ul { height: 0px; transition: height 0.5s ease-out; } div ul { height: 80px; transition: height 0.5s ease-in; }
注意 ul 一定要有 height 的具體值,如果沒(méi)有具體指明多少 px,則雖然能折疊,但是沒(méi)有動(dòng)畫效果。
最后就是利用 jQuery 或 ezj 切換 className 了,當(dāng)點(diǎn)擊 div 的時(shí)候 toggleClass("collapsed")
。
總結(jié)
以上所述是小編給大家介紹的純css實(shí)現(xiàn)多級(jí)折疊菜單折疊樹(shù)效果,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
- 這篇文章主要介紹了CSS多級(jí)菜單的實(shí)現(xiàn)代碼的相關(guān)資料,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-11-08