2025-03-22

作业分析

本次要求在编写一个简易的选项卡效果效果如下图


image.png

image.png

image.png

代码实现

<!DOCTYPE html>
<html lang="th">

<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 {
            height: 100%;
            width: 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 orange;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .tt {
            flex: 1;
            height: 5rem;
            background-color: #fff;
            font-size: 1.8rem;
            text-align: center;
            line-height: 5rem;
        }

        .tt.active,
        .tt:hover {
            background-color: orange;
            color: white;
        }

        .tab-content {
            width: 100%;
            height: calc(100% - 5rem);
            background-color: aquamarine;
            position: relative;
        }

        .tc {
            width: 100%;
            height: 100%;
            font-size: 1.8rem;
            text-align: center;
            color: white;
            position: absolute;
            display: none;
        }

        .tc.active {
            display: block;
        }

        .tc:nth-of-type(1) {
            background-color: blue;
        }

        .tc:nth-of-type(2) {
            background-color: green;
        }

        .tc:nth-of-type(3) {
            background-color: red;
        }
    </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 active">内容部分1</div>
            <div class="tc">内容部分2</div>
            <div class="tc">内容部分3</div>
        </div>
    </div>
    <script>
        let tts = document.getElementsByClassName("tt")
        for (let i = 0; i < tts.length; i++) {
            tts[i].onmouseenter = function () {
                for (let j = 0; j < tts.length; j++) {
                    tts[j].classList = "tt"
                }
                tts[i].classList = "tt active"
                let tcs = document.getElementsByClassName("tc")
                for (let x = 0; x < tcs.length; x++) {
                    tcs[x].classList = "tc"
                }
                tcs[i].classList = "tc active"
            }
        }
    </script>
</body>

</html>

个人总结

利用好循环结构可以简化很多步骤,勤加练习熟悉循环结构,编写更多作品

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

推荐阅读更多精彩内容

  • 避免脑卒中四大要素 脑卒中,又称中风,是一种严重威胁人类健康的疾病。了解并掌握避免脑卒中的关键要素,对我们的健康至...
    期颐堂阅读 15评论 0 0
  • 深入解析SSL/TLS证书:构建现代网络安全的密码学基石1.1 TLS 1.3握手流程优化 (此处展示TLS 1....
    网安秘谈阅读 35评论 0 0
  • 页面展示 代码 <!DOCTYPE html> Document /* 公共样式 */ ...
    CC言堇阅读 17评论 0 0
  • 腰间盘退行性变是指腰椎间盘随年龄增长自然老化,导致结构功能逐渐退化的生理病理过程。这一过程涉及椎间盘纤维环、髓核、...
    纸壳子123阅读 52评论 0 0
  • 筑基课清盘收获:基础心理学需要和动机、人格和气质 第一单元需要的概述, 需要:有机体内部的一种不平衡状态。特点包括...
    呼建荣阅读 35评论 0 0