效果展示

屏幕截图 2025-03-28 200304.png
代码实现
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>demo02</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
}
body {
font-size: 10px;
}
.tab {
width: 450px;
height: 300px;
border: solid 2px #daae1e;
}
.tab-title {
width: 100%;
height: 30px;
border-bottom: #daae1e 1px solid;
display: flex;
justify-content: space-between;
}
.tt {
width: 33%;
height: 100%;
color: #aaeb11;
text-align: center;
line-height: 30px;
cursor: pointer;
}
.tt:hover {
background-color: #daae1e;
color: #fff;
}
.tab-content {
width: 100%;
height: 270px;
position: relative;
}
.tc {
width: 100%;
height: 270px;
position: absolute;
display: none;
text-align: center;
}
.tc:nth-of-type(1) {
background-color: bisque;
display: block;
}
.tc:nth-of-type(2) {
background-color: brown;
}
.tc:nth-of-type(3) {
background-color: chartreuse;
}
</style>
</head>
<body>
<div class="tab">
<div class="tab-title">
<div class="tt">标题一</div>
<div class="tt">标题二</div>
<div class="tt">标题三</div>
</div>
<div class="tab-content">
<div class="tc">内容一</div>
<div class="tc">内容二</div>
<div class="tc">内容三</div>
</div>
</div>
</body>
<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++) {
tts[x].className = "tt"
}
tts[i].classList = "tt active"
}
}
</script>
</html>