···
<!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;
}
.tab{
width: 40rem;
height: 30rem;
border: solid 1px #000;
}
.tab-title{
width: 100%;
height: 5rem;
background-color: antiquewhite;
border-bottom: solid 2px #7d1818 ;
display: flex;
justify-content: space-between;
align-items: center;
}
.tt{
flex: 1;
height: 5rem;
background-color: rgb(90, 179, 214);
font-size: 1.8rem;
text-align: center;
line-height: 5rem;
}
/* .tt和.active中间不加空格,连着写-表示这两个名称出现在同一个标签中*/
/* <div class="tt active"></div> */
.tt.active,
.tt:hover{
background-color: #7d1818;
color: #fff;
}
.tab-content{
width: 100%;
height:calc(100% - 5rem); /*calculation 计算*/
/* height: 50px; */
background-color: #75a3d1;
position: relative;
}
.tc{
width: 100%;
height: 100%;
position: absolute;
font-size: 1.8rem;
text-align: center;
display: none;
color: #fff;
}
.tc.active{
display: block;
}
.tc:nth-of-type(1) {
background-color: #5584b3;
}
.tc:nth-of-type(2) {
background-color:red;
}
</style>
</head>
<body>
<div class="tab">
<div class="tab-title">
<div class="tt active">标题一</div>
<div class="tt">标题二</div>
<div class="tt">标题三</div>
</div>
<div class="tab-content">
<div class="tc active">内容一</div>
<div class="tc">内容二</div>
<div class="tc">内容三</div>
</div>
</div>
<script>
let tts = document.getElementsByClassName('tt');
for(let i = 0; i < tts.length; i++){
tts[i].onmouseenter = function(){
// console.log("鼠标进入了编号为", i , "的标题");
for(let j = 0; j < tts.length; j++){
tts[j].className = 'tt';
}
tts[i].className = 'tt active';
let tcs = document.getElementsByClassName('tc');
for(let k = 0; k < tcs.length; k++){
tcs[k].className = 'tc';
}
tcs[i].className = 'tc active';
}
}
</script>
</body>
</html>
效果展示
屏幕截图 2025-03-29 133307.png