CSS實(shí)現(xiàn)導(dǎo)航條Tab切換的三種方法介紹
前面的話
導(dǎo)航條Tab在頁面中非常常見,本文說詳細(xì)介紹CSS實(shí)現(xiàn)導(dǎo)航條Tab的三種方法
布局

根據(jù)上圖所示,先規(guī)定幾個定義,上圖的模塊整體叫做導(dǎo)航,由導(dǎo)航標(biāo)題和導(dǎo)航內(nèi)容組成。要實(shí)現(xiàn)上圖所示的布局效果,有兩種布局方法:語義布局和視覺布局
【語義布局】
從語義布局的角度來看,每一個導(dǎo)航標(biāo)題和其對應(yīng)的導(dǎo)航內(nèi)容應(yīng)該是一個整體
<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;}
</style>
<div class="box">
<ul class="nav">
<li class="navI navI_active">
<h2 class="navI-tit">課程</h2>
<p class="navI-txt">課程內(nèi)容</p>
</li>
<li class="navI">
<h2 class="navI-tit">學(xué)習(xí)計(jì)劃</h2>
<p class="navI-txt ml1">學(xué)習(xí)計(jì)劃內(nèi)容</p>
</li>
<li class="navI">
<h2 class="navI-tit">技能圖譜</h2>
<p class="navI-txt ml2">技能圖譜內(nèi)容</p>
</li>
</ul>
</div>
【視覺布局】
從視覺布局的角度來看,所有導(dǎo)航標(biāo)題為一組,所有導(dǎo)航內(nèi)容為一組
<style>
body,p{margin: 0;}
ul{margin: 0;padding: 0;list-style: none;}
a{text-decoration: none;color: inherit;}
.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;text-indent: 2em; line-height: 2;}
.nav-txtI{height: 200px;}
</style>
<div class="box">
<nav class="nav-tit">
<a class="nav-titI">課程</a>
<a class="nav-titI">學(xué)習(xí)計(jì)劃</a>
<a class="nav-titI">技能圖譜</a>
</nav>
<ul class="nav-txt">
<li class="nav-txtI nav-txtI_active">課程內(nèi)容</li>
<li class="nav-txtI">學(xué)習(xí)計(jì)劃內(nèi)容</li>
<li class="nav-txtI">技能圖譜內(nèi)容</li>
</ul>
</div>
hover
導(dǎo)航條的功能就是點(diǎn)擊導(dǎo)航標(biāo)題時(shí),顯示對應(yīng)的導(dǎo)航內(nèi)容。如果使用偽類hover實(shí)現(xiàn)類似效果,使用第一種布局方式語義布局比較合適
由于在語義布局中,三個導(dǎo)航內(nèi)容是處于重疊的狀態(tài)。移入其父元素.navI時(shí),觸發(fā)鼠標(biāo)的hover態(tài),給父元素添加樣式為position:relative;z-index:1;。從而提升了層級z-index。在其子元素導(dǎo)航內(nèi)容的層級比拼中,“子憑父貴”,父元素層級高的,其導(dǎo)航內(nèi)容在重疊狀態(tài)中顯示在最上面
<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;}
/*重點(diǎn)代碼*/
.navI:hover{position:relative;z-index:1;}
.navI:hover .navI-tit{background:#fff;border-bottom:none;}
</style>
<div class="box">
<ul class="nav">
<li class="navI navI_active">
<h2 class="navI-tit">課程</h2>
<p class="navI-txt">課程內(nèi)容</p>
</li>
<li class="navI">
<h2 class="navI-tit">學(xué)習(xí)計(jì)劃</h2>
<p class="navI-txt ml1">學(xué)習(xí)計(jì)劃內(nèi)容</p>
</li>
<li class="navI">
<h2 class="navI-tit">技能圖譜</h2>
<p class="navI-txt ml2">技能圖譜內(nèi)容</p>
</li>
</ul>
</div>
[缺點(diǎn)]:初始狀態(tài)時(shí),第一個導(dǎo)航標(biāo)題無法實(shí)現(xiàn)默認(rèn)被選中的狀態(tài)(背景白色,無下劃線);鼠標(biāo)移出導(dǎo)航模塊時(shí),導(dǎo)航內(nèi)容部分無法固定,顯示第一個導(dǎo)航內(nèi)容;鼠標(biāo)移出導(dǎo)航模塊時(shí),導(dǎo)航標(biāo)題的樣式無法固定,恢復(fù)到默認(rèn)狀態(tài)
錨點(diǎn)
實(shí)現(xiàn)導(dǎo)航條的關(guān)鍵就在于如何建立導(dǎo)航標(biāo)題與導(dǎo)航內(nèi)容之間的聯(lián)系,而錨點(diǎn)就可以實(shí)現(xiàn)類似效果。通過點(diǎn)擊錨點(diǎn),頁面生成一個哈希值,然后跳轉(zhuǎn)到相應(yīng)內(nèi)容的位置
使用錨點(diǎn)技術(shù)時(shí),使用語義布局和視覺布局都可以實(shí)現(xiàn)
【1】使用語義布局
使用語義布局時(shí),可以使用偽類target,通過target選擇器來改變點(diǎn)擊導(dǎo)航標(biāo)題時(shí),當(dāng)前標(biāo)題的樣式。不僅如此,因?yàn)橐褂眯值苓x擇器,所以需要改變HTML結(jié)構(gòu),將導(dǎo)航標(biāo)題的HTML結(jié)構(gòu)移到導(dǎo)航內(nèi)容的下面
點(diǎn)擊導(dǎo)航標(biāo)題時(shí),觸發(fā)target偽類,改變對應(yīng)的導(dǎo)航內(nèi)容的層級z-index,從而使當(dāng)前導(dǎo)航內(nèi)容在三個導(dǎo)航內(nèi)容中勝出,在最上層顯示;與此同時(shí),改變當(dāng)前導(dǎo)航標(biāo)題的樣式
<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;}
/*重點(diǎn)代碼*/
.navI-txt:target{position:relative;z-index:1;}
.navI-txt:target ~ .navI-tit{background:#fff;border-bottom:none;}
</style>
<div class="box">
<ul class="nav">
<li class="navI navI_active">
<p class="navI-txt" id="kc">課程內(nèi)容</p>
<a class="navI-tit" href="#kc">課程</a>
</li>
<li class="navI">
<p class="navI-txt ml1" id="xx">學(xué)習(xí)計(jì)劃內(nèi)容</p>
<a class="navI-tit" href="#xx">學(xué)習(xí)計(jì)劃</a>
</li>
<li class="navI">
<p class="navI-txt ml2" id="jn">技能圖譜內(nèi)容</p>
<a class="navI-tit" href="#jn">技能圖譜</a>
</li>
</ul>
</div>
[缺點(diǎn)]:初始態(tài)默認(rèn)選中的導(dǎo)航標(biāo)題樣式無法設(shè)置;改變了HTML結(jié)構(gòu);錨點(diǎn)技術(shù)本身的局限是錨點(diǎn)目標(biāo)會盡可能的到達(dá)可視區(qū)域上方,從而可能會生成頁面跳動
【2】使用視覺布局
在視覺布局中,三個導(dǎo)航內(nèi)容屬于同一個父元素,與父元素的高度相同,并按照塊級元素的排列方式進(jìn)行排布,父元素設(shè)置溢出隱藏時(shí),默認(rèn)只顯示第一個導(dǎo)航內(nèi)容
點(diǎn)擊導(dǎo)航標(biāo)題時(shí),對應(yīng)的導(dǎo)航內(nèi)容到達(dá)導(dǎo)航標(biāo)題行下面,達(dá)到了導(dǎo)航切換的效果
使用偽類hover來實(shí)現(xiàn)改變當(dāng)前導(dǎo)航標(biāo)題樣式的效果
<style>
body,p{margin: 0;}
ul{margin: 0;padding: 0;list-style: none;}
a{text-decoration: none;color: inherit;}
.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;text-indent: 2em; line-height: 2;}
.nav-txtI{height: 200px;}
/*重點(diǎn)內(nèi)容*/
.nav-txt{overflow: hidden;}
.nav-titI:hover{background-color: white;border-bottom: none;}
</style>
<div class="box">
<nav class="nav-tit">
<a class="nav-titI" href="#kc">課程</a>
<a class="nav-titI" href="#xx">學(xué)習(xí)計(jì)劃</a>
<a class="nav-titI" href="#jn">技能圖譜</a>
</nav>
<ul class="nav-txt">
<li class="nav-txtI nav-txtI_active" id="kc">課程內(nèi)容</li>
<li class="nav-txtI" id="xx">學(xué)習(xí)計(jì)劃內(nèi)容</li>
<li class="nav-txtI" id="jn">技能圖譜內(nèi)容</li>
</ul>
</div>
[缺點(diǎn)]:初始態(tài)默認(rèn)選中的導(dǎo)航標(biāo)題樣式無法設(shè)置;錨點(diǎn)技術(shù)本身的局限是錨點(diǎn)目標(biāo)會盡可能的到達(dá)可視區(qū)域上方,從而可能會生成頁面跳動;hover態(tài)與點(diǎn)擊態(tài)分開,可能會讓人犯暈;鼠標(biāo)移出導(dǎo)航模塊時(shí),導(dǎo)航標(biāo)題的樣式無法固定,恢復(fù)到默認(rèn)狀態(tài)
label
上面使用錨點(diǎn)技術(shù)來聯(lián)系導(dǎo)航標(biāo)題和導(dǎo)航內(nèi)容,而label也可以實(shí)現(xiàn)類似的效果。label元素為input元素定義標(biāo)注,建立文字標(biāo)簽與表單控件的關(guān)聯(lián)。在label元素內(nèi)點(diǎn)擊文本會觸發(fā)此控件,選擇該文本時(shí)瀏覽器會自動把焦點(diǎn)轉(zhuǎn)到和標(biāo)簽相關(guān)的表單控件上
使用label時(shí),使用語義布局和視覺布局都可以實(shí)現(xiàn)
【1】使用語義布局
使用語義布局時(shí),使用label標(biāo)簽來顯示導(dǎo)航標(biāo)題,且需要配合使用單選按鈕<input type="radio">。使用偽類checked,通過checked選擇器來改變點(diǎn)擊導(dǎo)航標(biāo)題時(shí),當(dāng)前標(biāo)題的樣式。不僅如此,因?yàn)橐褂眯值苓x擇器,所以需要改變HTML結(jié)構(gòu),將單選按鈕放在每個.navI元素里的最上層,然后設(shè)置display:none,接下來是<label>表示導(dǎo)航標(biāo)題,最后是<p>表示導(dǎo)航內(nèi)容
點(diǎn)擊導(dǎo)航標(biāo)題時(shí),觸發(fā)checked偽類,改變對應(yīng)的導(dǎo)航內(nèi)容的層級z-index,從而使當(dāng)前導(dǎo)航內(nèi)容在三個導(dǎo)航內(nèi)容中勝出,在最上層顯示;與此同時(shí),改變當(dāng)前導(dǎo)航標(biāo)題的樣式
<style>
body,p{margin: 0;}
h2{margin: 0;font-size:100%;}
ul{margin: 0;padding: 0;list-style: none;}
input{margin: 0;width: 0;}
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{display:block;line-height: 40px;text-align: center;cursor: pointer;border-left: 1px solid #cecece;border-bottom: 1px solid #cecece;}
.navI-txt{position:relative;width: 572px;height:200px;text-indent:2em;line-height: 2;background:#fff;}
.ml1{margin-left: -100%;}
.ml2{margin-left: -200%;}
/*重點(diǎn)代碼*/
.navI-radio{display:none;}
.navI-radio:checked + .navI-tit{background:#fff;border-bottom:none;}
.navI-radio:checked ~ .navI-txt{z-index:1;}
</style>
<div class="box">
<ul class="nav">
<li class="navI">
<input class="navI-radio" name="nav" type="radio" id="kc" checked>
<label class="navI-tit" for="kc">課程</label>
<p class="navI-txt">課程內(nèi)容</p>
</li>
<li class="navI">
<input class="navI-radio" name="nav" type="radio" id="xx">
<label class="navI-tit" for="xx">學(xué)習(xí)計(jì)劃</label>
<p class="navI-txt ml1">學(xué)習(xí)計(jì)劃內(nèi)容</p>
</li>
<li class="navI">
<input class="navI-radio" name="nav" type="radio" id="jn">
<label class="navI-tit" for="jn">技能圖譜</label>
<p class="navI-txt ml2">技能圖譜內(nèi)容</p>
</li>
</ul>
</div>
[缺點(diǎn)]:HTML結(jié)構(gòu)較復(fù)雜
【2】使用視覺布局
在視覺布局中,三個導(dǎo)航內(nèi)容屬于同一個父元素,與父元素的高度相同,并按照塊級元素的排列方式進(jìn)行排布,父元素設(shè)置溢出隱藏時(shí),默認(rèn)只顯示第一個導(dǎo)航內(nèi)容
點(diǎn)擊導(dǎo)航標(biāo)題時(shí),對應(yīng)的導(dǎo)航內(nèi)容到達(dá)導(dǎo)航標(biāo)題行下面,達(dá)到了導(dǎo)航切換的效果
使用偽類hover來實(shí)現(xiàn)改變當(dāng)前導(dǎo)航標(biāo)題樣式的效果
<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;}
/*重點(diǎn)內(nèi)容*/
.nav-txt{overflow: hidden;}
.nav-titI:hover{background-color: #fff;border-bottom:none;}
</style>
<div class="box">
<nav class="nav-tit">
<label class="nav-titI" for="kc">課程</label>
<label class="nav-titI" for="xx">學(xué)習(xí)計(jì)劃</label>
<label class="nav-titI" for="jn">技能圖譜</label>
</nav>
<nav class="nav-txt">
<input class="nav-txtI nav-txtI_active" id="kc" value="課程內(nèi)容" readonly>
<input class="nav-txtI" id="xx" value="學(xué)習(xí)計(jì)劃內(nèi)容" readonly>
<input class="nav-txtI" id="jn" value="技能圖譜內(nèi)容" readonly>
</nav>
</div>
[缺點(diǎn)]:初始態(tài)默認(rèn)選中的導(dǎo)航標(biāo)題樣式無法設(shè)置;有時(shí)會出現(xiàn)頁面跳動的效果;hover態(tài)與點(diǎn)擊態(tài)分開,可能會讓人犯暈;鼠標(biāo)移出導(dǎo)航模塊時(shí),導(dǎo)航標(biāo)題的樣式無法固定,恢復(fù)到默認(rèn)狀態(tài)
最后
上面的三種方法中,實(shí)現(xiàn)效果最好的是使用label標(biāo)簽配合radio類型的input標(biāo)簽,通過:checked選擇器來實(shí)現(xiàn)
在實(shí)際應(yīng)用中,使用javascript的方式來控制導(dǎo)航條Tab的情況更為普遍
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
利用純CSS3實(shí)現(xiàn)tab選項(xiàng)卡切換示例代碼
這篇文章給大家介紹了如何利用純CSS3實(shí)現(xiàn)tab切換,實(shí)現(xiàn)的效果很簡單但很使用,而且代碼也不復(fù)雜,有需要的朋友們可以參考借鑒。2016-09-21一款純css3實(shí)現(xiàn)的tab選項(xiàng)卡的實(shí)列教程
css3怎么做tab選項(xiàng)卡?下面費(fèi)大家分享一款純css3實(shí)現(xiàn)的tab選項(xiàng)卡的實(shí)列教程,單擊左側(cè)的選項(xiàng)的時(shí)候,右側(cè)內(nèi)容以動畫的形式展示。下文有具體代碼,想學(xué)的朋友可以進(jìn)來參考一2014-12-11
CSS3華麗的Tab菜單當(dāng)鼠標(biāo)滑過時(shí)會出現(xiàn)展開動畫
這款Tab菜單的菜單項(xiàng)是一個個小圖標(biāo),鼠標(biāo)滑過時(shí),菜單項(xiàng)展示對應(yīng)文字,并出現(xiàn)展開的動畫2014-05-04
CSS3 Tab動畫實(shí)例之背景切換動態(tài)效果
這篇文章主要介紹了CSS3 Tab動畫實(shí)例之背景切換動態(tài)效果,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-08-20



