2025-03-22

作業(yè)分析:選項卡開發(fā)

使用js實現(xiàn)一個選項卡效果

HTML標簽

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>選項卡開發(fā)</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>
        <!-- 內(nèi)容 -->
        <div class="tab-content">
            <div class="tc">內(nèi)容1</div>
            <div class="tc">內(nèi)容2</div>
            <div class="tc">內(nèi)容3</div>
        </div>
    </div>

    <!-- JS代碼 -->
    <script>
        let tts = document.getElementsByClassName("tt")
        // 循環(huán)
        for (let i = 0; i < tts.length; i++) {
            tts[i].onmouseenter = function() {
                console.log("鼠標進入了第" + i + "個標題")
                // 獲取所有的內(nèi)容標簽
                let tcs = document.getElementsByClassName("tc")
                // 隱藏所有的內(nèi)容標簽
                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;
}

/* 樣式繪制選項卡區(qū)域 */
.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);
}
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

  • 作業(yè)分析 本次要求在編寫一個簡易的選項卡效果效果如下圖 代碼實現(xiàn) 個人總結 利用好循環(huán)結構可以簡化很多步驟,勤加練...
    圓起圓落閱讀 38評論 0 2
  • 作業(yè)分析:下拉菜單效果 html標簽 css
    寫不出代碼的程序員閱讀 28評論 0 0
  • 頁面展示 代碼 <!DOCTYPE html> Document /* 公共樣式 */ ...
    CC言堇閱讀 41評論 0 0
  • 云計算管理系統(tǒng)開發(fā)作業(yè): 1.作業(yè)效果: 2.代碼: <!DOCTYPE html> Documen...
    WwYiihung閱讀 34評論 0 0
  • 【摘要】如何借助智慧教育平臺豐富的場景支撐與強大的資源支持,來推動課題項目研究,我們探索出了“一核五徑四層”的“1...
    吳國花閱讀 94評論 0 0

友情鏈接更多精彩內(nèi)容