CSS如何?Tab頁切換的代碼分享

來源:互聯網
上載者:User
Tab頁面切換的功能我們已經和大家分享過很多,本文我們主要和大家紹CSS實現Tab頁切換執行個體代碼,需要的朋友可以參考下,希望能協助到大家。

1.hover

移入其父元素.navI時,觸發滑鼠的hover態,給父元素添加樣式為position:relative;z-index:1;。從而提升了層級z-index。在其子項目導航內容的層級比拼中,“子憑父貴”,父元素層級高的,其導航內容在重疊狀態中顯示在最上面


<style>body,p{margin: 0;}h2{margin: 0;font-size:100%;}ul{margin: 0;padding: 0;list-style: none;}   a{text-decoration: none;color:inherit;}.box{width: 572px;border: 1px solid #999;overflow: hidden;}.nav{margin-left: -1px;font: 14px "微軟雅黑";overflow: hidden;background-color: #f1f1f1;}.navI{float: left;width: 33.333%;box-sizing: border-box;}.navI-tit{line-height: 40px;text-align: center;cursor: pointer;border-left: 1px solid #cecece;border-bottom: 1px solid #cecece;}.navI-txt{width: 572px;height:200px;text-indent:2em;line-height: 2;background:#fff;}.ml1{margin-left: -100%;}.ml2{margin-left: -200%;}.navI_active{position:relative;z-index:1;}/*重點代碼*/.navI:hover{position:relative;z-index:1;}.navI:hover .navI-tit{background:#fff;border-bottom:none;}</style><p class="box">    <ul class="nav">        <li class="navI navI_active">            <h2 class="navI-tit">課程</h2>            <p class="navI-txt">課程內容</p>        </li>        <li class="navI">            <h2 class="navI-tit">學習計劃</h2>            <p class="navI-txt ml1">學習計劃內容</p>        </li>        <li class="navI">            <h2 class="navI-tit">技能圖譜</h2>            <p class="navI-txt ml2">技能圖譜內容</p>        </li>    </ul>   </p>

2.錨點

點擊導航標題時,觸發target偽類,改變對應的導航內容的層級z-index,從而使當前置航內容在三個導航內容中勝出,在最上層顯示;與此同時,改變當前置航標題的樣式


<style>body,p{margin: 0;}h2{margin: 0;font-size:100%;}ul{margin: 0;padding: 0;list-style: none;}   a{text-decoration: none;color:inherit;}.box{width: 572px;border: 1px solid #999;overflow: hidden;}.nav{margin-left: -1px;font: 14px "微軟雅黑";overflow: hidden;background-color: #f1f1f1;}.navI{float: left;width: 33.333%;box-sizing: border-box;position:relative;}.navI-tit{position:absolute;top:0;left:0;right:0;box-sizing: border-box;line-height: 40px;height: 40px;text-align: center;cursor: pointer;border-left: 1px solid #cecece;border-bottom: 1px solid #cecece;}.navI-txt{width: 572px;height:200px;margin-top: 40px;text-indent:2em;line-height: 2;background:#fff;}.ml1{margin-left: -100%;}.ml2{margin-left: -200%;}.navI_active{z-index:1;}/*重點代碼*/.navI-txt:target{position:relative;z-index:1;}.navI-txt:target ~ .navI-tit{background:#fff;border-bottom:none;}</style><p class="box">    <ul class="nav">        <li class="navI navI_active">            <p class="navI-txt" id="kc">課程內容</p>            <a class="navI-tit" href="#kc">課程</a>        </li>        <li class="navI">            <p class="navI-txt ml1" id="xx">學習計劃內容</p>            <a class="navI-tit" href="#xx">學習計劃</a>        </li>        <li class="navI">            <p class="navI-txt ml2" id="jn">技能圖譜內容</p>            <a class="navI-tit" href="#jn">技能圖譜</a>        </li>    </ul>   </p>

3.radio

點擊導航標題時,觸發checked偽類,改變對應的導航內容的層級z-index,從而使當前置航內容在三個導航內容中勝出,在最上層顯示;與此同時,改變當前置航標題的樣式


<style>body,p{margin: 0;}ul{margin: 0;padding: 0;list-style: none;}a{text-decoration: none;color: inherit;}input{margin: 0;padding: 0;border:none;}.box{width:572px;border:1px solid #999;font:14px "微軟雅黑";overflow:hidden;}.nav-tit{margin-left: -1px;height: 40px;line-height: 40px;text-align: center;background-color: #f1f1f1;overflow: hidden;}.nav-titI{box-sizing: border-box;float: left;width: 33.333%;border-left: 1px solid #cecece;border-bottom: 1px solid #cecece;cursor: pointer;}.nav-txt{height: 200px;}.nav-txtI{height: 200px;display:block;width: 100%;text-indent: 2em; line-height: 2;}/*重點內容*/.nav-txt{overflow: hidden;}.nav-titI:hover{background-color: #fff;border-bottom:none;}</style><p class="box">    <nav class="nav-tit">        <label class="nav-titI" for="kc">課程</label>        <label class="nav-titI" for="xx">學習計劃</label>        <label class="nav-titI" for="jn">技能圖譜</label>    </nav>    <nav class="nav-txt">        <input class="nav-txtI nav-txtI_active" id="kc" value="課程內容" readonly>        <input class="nav-txtI" id="xx" value="學習計劃內容" readonly>        <input class="nav-txtI" id="jn" value="技能圖譜內容" readonly>    </nav></p>
相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.