選項(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>