最近切一個頁面的時候涉及到了一個tab切換的部分,由於不想用js想着能不能用純CSS的選擇器來實現切換效果。搜了一下大體有下面三種寫法。css
:hover
選擇器
a標籤的錨點 + :target選擇器
label和radio
的綁定關係和radio選中時的:checked
來實現效果
通過實驗發現第三種方法達到的效果最好。因此下面講一下第三種實現的方法。html
這種方法的寫法不固定,我查資料的時候各類各樣的寫法都有一度讓我一頭霧水的。最後看完發現整體思路都是同樣的,無非就是下面的幾個步驟。git
display:none;
隱藏的、設置絕對定位,將left設置爲很大的負值,移動到頁面外達到隱藏效果、設置**絕對定位:使元素脫離文檔流,而後opacity: 0;
**設置爲透明來達到隱藏效果。z-index
設置層級關係來相互遮擋。checked="checked"
屬性+
選擇器 和 ~
選擇器來設置選中對應元素時下方的tab頁的樣式,來達到選中的效果/* 當radio爲選中狀態時設置它的test-label兄弟元素的屬性 */
input[type="radio"]:checked+.test-label {
/* 爲了修飾存在的邊框背景屬性 */
border-color: #cbcccc;
border-bottom-color: #fff;
background: #fff;
/* 爲了修飾存在的層級使下邊框遮擋下方div的上邊框 */
z-index: 10;
}
/* 當radio爲選中狀態時設置與它同級的tab-box元素的顯示層級 */
input[type="radio"]:checked~.tab-box {
/* 選中時提高層級,遮擋其餘tab頁達到選中切換的效果 */
z-index: 5;
}
複製代碼
這樣就能夠實現一個Tab頁切換的效果了,不用一點兒js,固然確定也有兼容性的問題。實際操做中tab頁仍是使用js比較好。下面是小Demo的代碼,樣式比較多主要是爲了實現各類選中效果,真正用來達到選擇切換目地的核心代碼就幾行github
演示地址ui
代碼:spa
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>CSS實現Tab切換效果</title>
<style> ul { margin: 0; padding: 0; } .clearfloat { zoom: 1; } .clearfloat::after { display: block; clear: both; content: ""; visibility: hidden; height: 0; } .tab-list { position: relative; } .tab-list .tab-itom { float: left; list-style: none; margin-right: 4px; } .tab-itom .test-label { position: relative; display: block; width: 85px; height: 27px; border: 1px solid transparent; border-top-left-radius: 5px; border-top-right-radius: 5px; line-height: 27px; text-align: center; background: #e7e8eb; } .tab-itom .tab-box { /* 設置絕對定位方便定位相對於tab-list欄的位置,同時爲了可使用z-index屬性 */ position: absolute; left: 0; top: 28px; width: 488px; height: 248px; border: 1px solid #cbcccc; border-radius: 5px; border-top-left-radius: 0px; background: #fff; /* 設置層級最低方便選中狀態遮擋 */ z-index: 0; } /* 用絕對定位使按鈕脫離文檔流,透明度設置爲0將其隱藏 */ input[type="radio"] { position: absolute; opacity: 0; } /* 利用選擇器實現 tab切換 */ /* 當radio爲選中狀態時設置它的test-label兄弟元素的屬性 */ input[type="radio"]:checked + .test-label { /* 爲了修飾存在的邊框背景屬性 */ border-color: #cbcccc; border-bottom-color: #fff; background: #fff; /* 爲了修飾存在的層級使下邊框遮擋下方div的上邊框 */ z-index: 10; } /* 當radio爲選中狀態時設置與它同級的tab-box元素的顯示層級 */ input[type="radio"]:checked ~ .tab-box { /* 選中時提高層級,遮擋其餘tab頁達到選中切換的效果 */ z-index: 5; } </style>
</head>
<body class="clearfloat">
<ul class="tab-list clearfloat">
<li class="tab-itom">
<input type="radio" id="testTabRadio1" class="test-radio" name="tab" checked="checked">
<label class="test-label" for="testTabRadio1">選項卡一</label>
<div class="tab-box">
111111111111
</div>
</li>
<li class="tab-itom">
<input type="radio" id="testTabRadio2" class="test-radio" name="tab">
<label class="test-label" for="testTabRadio2">選項卡二</label>
<div class="tab-box">
2222222222222
</div>
</li>
<li class="tab-itom">
<input type="radio" id="testTabRadio3" class="test-radio" name="tab">
<label class="test-label" for="testTabRadio3">選項卡三</label>
<div class="tab-box">
33333333333333
</div>
</li>
</ul>
</body>
</html>
複製代碼