选项卡代码示例
<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;}
/* 样式绘制选项卡区域 */
.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">
<!-- 标题 -->
<div class="tab-title">
<div class="tt active">标题1</div>
<div class="tt">标题2</div>
<div class="tt">标题3</div>
</div>
<!-- 内容 -->
<div class="tab-content">
<div class="tc">内容1</div>
<div class="tc">内容2</div>
<div class="tc">内容3</div>
</div>
</div>
<!-- JS代码 -->
<script>
// 获取所有的标题标签
let tts = document.getElementsByClassName("tt")
// 循环给每个标题添加事件
for (let i = 0; i < tts.length; i++) {
// 给每个标题添加鼠标进入事件
tts[i].onmouseenter = function() {
console.log("鼠标进入了第" + i + "个标题")
// 获取所有的内容标签
let tcs = document.getElementsByClassName("tc")
// 隐藏所有的内容标签
for(let j = 0; j < tcs.length; j++) {
tcs[j].style.display = "none"
}
// 显示和标题编号相同的内容标签
tcs[i].style.display = "block"
// 高亮对应标题 :取消所有标题的高亮
for(let x = 0; x < tts.length; x ++) {
// 让所有的标题class="tt"
tts[x].classList = "tt"
}
// 让对应编号的标题,添加active名称
tts[i].classList = "tt active"
}
}
</script>
</body>
</html>