作業(yè)分析

選項卡開發(fā) 2025-03-23 191101.png
代碼演示
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>選項卡</title>
<style>
/* 公共樣式 */
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
html,body{
width: 100%;
height: 100%;
}
html{
font-size: 10px;
}
/* 樣式繪制選項卡區(qū)域 */
.tab{
width: 40rem;
height: 30rem;
border:solid 2px oldlace;
border-radius: 8px;
overflow: hidden;
}
.tab-tittle{
width: 100%;
height: 5rem;
display: flex;
justify-content:space-between;
border-bottom: solid 1px oldlace;
}
.tt{
border-right: solid 1px oldlace;
text-align: center;
width: 33%;
height: 5rem;
line-height: 5rem;
font-size: 1.8rem;
cursor: pointer;
}
.tab-tittle .tt:last-child{
border-right: none;
}
.tt:hover{
background-color: oldlace;
color: white;
}
.tab-content{
height: 100%;
width: 100%;
position: relative;
overflow: hidden;
background-color: azure;
}
.tab-content .tc{
width: 100%;
height: 100%;
color: white;
font-size: 1.6rem;
text-align: center;
position: absolute;
display: none;
}
.tc:nth-of-type(1){
background-color: aqua;
display: block;
}
.tc:nth-of-type(2){
background-color: bisque;
}
.tc:nth-of-type(3){
background-color:beige;
}
</style>
</head>
<body>
<div class="tab">
<!-- 標題 -->
<div class="tab-tittle">
<div class="tt active">標題1</div>
<div class="tt">標題2</div>
<div class="tt">標題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>
//獲取所有的標題標簽
let tts = document.getElementsByClassName("tt")
//循環(huán)給每個標題添加事件
for(let i = 0;i < tts.length;i ++){
//給每個標題添加鼠標進入事件
tts[i].onmouseenter = function(){
console.log("鼠標進入了第" + i + "個標題")
//獲取所有的內(nèi)容標簽
let tcs = document.getElementsByClassName("tc")
//隱藏所有的內(nèi)容標簽
for( let j = 0;j < tts.length;j ++){
tcs[j].style.display = "none"
}
//顯示和標題編號相同的內(nèi)容標簽
tcs[i].style.display="block"
//高亮對應(yīng)標題:取消所有標題的高亮
for(let x=0;x<tts.length;x ++){
tts[x].classList="tt"
}
//讓對應(yīng)編號的標題,添加active名稱
tts[i].classList="tt active"
}
}
</script>
</body>
</html>