2025-03-22

作业分析:选项卡开发

使用js实现一个选项卡效果

HTML标签

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>选项卡开发</title>
    <link rel="stylesheet" href="./css/demo01.css">
</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++) {
                    tts[x].classList = "tt" 
                }
                tts[i].classList = "tt active"
            }
        }
    </script>
</body>
</html>

CSS

/* 公共样式 */
* {
    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;
    font-size: 20px;
    font-weight: 600;
}

.tc:nth-of-type(1) {
    display: block;
    background-color: rgb(25, 227, 160);
}

.tc:nth-of-type(2) {
    background-color: rgb(50, 72, 182);
}

.tc:nth-of-type(3) {
    background-color: rgb(41, 219, 219);
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 作业分析 本次要求在编写一个简易的选项卡效果效果如下图 代码实现 个人总结 利用好循环结构可以简化很多步骤,勤加练...
    圆起圆落阅读 37评论 0 2
  • 作业分析:下拉菜单效果 html标签 css
    写不出代码的程序员阅读 19评论 0 0
  • 页面展示 代码 <!DOCTYPE html> Document /* 公共样式 */ ...
    CC言堇阅读 32评论 0 0
  • 云计算管理系统开发作业: 1.作业效果: 2.代码: <!DOCTYPE html> Documen...
    WwYiihung阅读 32评论 0 0
  • 【摘要】如何借助智慧教育平台丰富的场景支撑与强大的资源支持,来推动课题项目研究,我们探索出了“一核五径四层”的“1...
    吴国花阅读 384评论 0 0