国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > CSS > 正文

純CSS實現可折疊樹狀菜單

2024-07-11 08:42:09
字體:
來源:轉載
供稿:網友

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:效果圖

 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 临夏市| 营口市| 葵青区| 林芝县| 荥阳市| 高雄县| 仁寿县| 玛纳斯县| 宁德市| 东乡| 托里县| 天峻县| 若尔盖县| 江油市| 雅江县| 永新县| 靖边县| 娄烦县| 仁寿县| 体育| 余江县| 稷山县| 博兴县| 千阳县| 青冈县| 上杭县| 依兰县| 莲花县| 塘沽区| 高淳县| 扶风县| 高要市| 孟州市| 慈利县| 法库县| 福泉市| 墨竹工卡县| 渭源县| 栖霞市| 安乡县| 陵水|