選項(xiàng)卡

選項(xiàng)卡代碼示例

<html lang="zh">
<head>
<meta charset="UTF-8">
5 <meta name="viewport" content="width=device-width, initial-scale=1.0">
6 <title>Document</title>
7 <style>
 /* 公共樣式 */
 *{margin: 0; padding: 0; box-sizing: border-box;}
 html, body{width: 100%;height: 100%;}
html{font-size: 10px;}

 /* 樣式繪制選項(xiàng)卡區(qū)域 */
 .tab{
 width: 40rem;
height: 30rem;
 border:solid 2px orange;
 }
 .tab-title{
 height:5rem;
 width:100%;
 border-bottom: solid 1px orange;

 display:flex;
 flex-direction: row;
justify-content: space-between;
 }
 .tt{
 height:5rem;
 line-height: 5rem;
  flex:1;
 text-align:center;
 font-size: 1.8rem;
}
 .tt:hover{
 background-color: orange;
 color:white;
 }
 .tt.active{
 background-color: orange;
 color:white;
 }
 .tab-content{
 width:100%;
 height:calc(100% - 5rem);
 background-color:aquamarine;
 position:relative;
 }
 .tc{
 position: absolute;
 display:none;
 height: 100%;
 width:100%;
 background-color: orange;
 text-align:center;
}
 .tc:nth-of-type(1){
 display:block; background-color:aquamarine;
 }
 .tc:nth-of-type(2){
 background-color:aqua;
 }
 .tc:nth-of-type(3){
 background-color:azure
 }
 </style>
 </head>
 <body>
 <div class="tab">
 <!-- 標(biāo)題 -->
 <div class="tab-title">
 <div class="tt active">標(biāo)題1</div>
 <div class="tt">標(biāo)題2</div>
 <div class="tt">標(biāo)題3</div>
 </div>
 <!-- 內(nèi)容 -->
 <div class="tab-content">
 <div class="tc">內(nèi)容1</div>
 <div class="tc">內(nèi)容2</div>
 <div class="tc">內(nèi)容3</div>
 </div>
 </div>

 <!-- JS代碼 -->
 <script>
 // 獲取所有的標(biāo)題標(biāo)簽
 let tts = document.getElementsByClassName("tt")

 // 循環(huán)給每個(gè)標(biāo)題添加事件
 for (let i = 0; i < tts.length; i++) {
 // 給每個(gè)標(biāo)題添加鼠標(biāo)進(jìn)入事件
 tts[i].onmouseenter = function() {
console.log("鼠標(biāo)進(jìn)入了第" + i + "個(gè)標(biāo)題")
// 獲取所有的內(nèi)容標(biāo)簽
let tcs = document.getElementsByClassName("tc")

// 隱藏所有的內(nèi)容標(biāo)簽
for(let j = 0; j < tcs.length; j++) {
 tcs[j].style.display = "none"
 }

 // 顯示和標(biāo)題編號(hào)相同的內(nèi)容標(biāo)簽
 tcs[i].style.display = "block"

 // 高亮對(duì)應(yīng)標(biāo)題 :取消所有標(biāo)題的高亮
 for(let x = 0; x < tts.length; x ++) {
 // 讓所有的標(biāo)題class="tt"
 tts[x].classList = "tt"
 }
 // 讓對(duì)應(yīng)編號(hào)的標(biāo)題,添加active名稱
 tts[i].classList = "tt active"
 }
}
 </script>
 </body>
 </html>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容