2025-03-29 選項(xiàng)卡效果展示

練習(xí)了一個(gè)選項(xiàng)卡樣式

  1. 框選選項(xiàng)卡的大邊框 給其加css樣式
    2.排列內(nèi)部?jī)?nèi)容,分為兩個(gè)部分,標(biāo)題和內(nèi)容,先弄標(biāo)題+css樣式
    3.圈定內(nèi)容部分 +css樣式
    4.js樣式,主要是獲取標(biāo)簽,循環(huán),改標(biāo)簽名稱
    用到的主要樣式
    css中
    height width
    border-bottom---- 加邊框下劃線
    text-align 文本居中
    line-height文本高度
    display:none隱藏內(nèi)容
    display:block 顯示內(nèi)容
    position 定位 相對(duì)relative 絕對(duì)absolute

js中
let tts=document.getElementsByClassName("tt")獲取文本
for循環(huán)
tts[i].classList="tt"所有標(biāo)題標(biāo)簽class都變?yōu)閠t

如圖效果


image.png
image.png
image.png

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

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>選項(xiàng)卡效果</title>

<link rel="stylesheet" href="./選項(xiàng)卡.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="tab-content">
<div class="tc active">1</div>
<div class="tc">2</div>
<div class="tc">3</div>
</div>
</div>
</div>
<script>
// js效果
// 步驟一:在tts變量中保存了三個(gè)標(biāo)題標(biāo)簽
let tts = document.getElementsByClassName("tt")
// console.log(tts)
// 步驟二:給需要的每個(gè)標(biāo)題標(biāo)簽添加鼠標(biāo)進(jìn)入事件
for (let i = 0; i < tts.length; i++) {
// console.log(tts[i])
tts[i].onmouseenter = function () {
// console.log("鼠標(biāo)進(jìn)入標(biāo)題為",i ,"的標(biāo)簽")
//步驟三: 鼠標(biāo)進(jìn)入標(biāo)題后,讓選中標(biāo)題高亮,不管之前是誰高亮,只要選中的高亮
// 去掉css中的active樣式名稱
for (let j = 0; j < tts.length; j++) {
// 操作class屬性,可以使用固定語法classList
// 讓所有標(biāo)題的class變?yōu)閠t不用active
tts[j].classList = "tt"

            }
            // 高亮選中的標(biāo)題
            tts[i].classList = "tt  active"
            // 循環(huán)內(nèi)容
            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>

css樣式

  • {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    }

/* 瀏覽器顯示高度,寬度100% */
html,
body {
height: 100%;
width: 100%;
}

/* 所有字體10px大小 */
html {
font-size: 10px;
}

.tab {
/* 一個(gè)rem=10個(gè)px /
width: 40rem;
height: 30rem;
/
加邊框,實(shí)線,一個(gè)像素,黑色 */
border: solid 1px #000;
}

.tab-title {
width: 100%;
height: 5rem;
/* 彈性樣式 /
display: flex;
/
兩邊對(duì)齊排列 /
justify-content: space-between;
/
文本居中 /
text-align: center;
/
文本高度 /
/
給邊框增加下劃線 /
border-bottom: solid 2px rgb(231, 150, 28);
/
background-color: paleturquoise; */
}

.tt {
height: 5rem;
/* 均勻分配寬度 /
flex: 1;
/
background-color: #fafafa; /
font-size: 1.8rem;
text-align: center;
/
文本高度,邊框高度=文本高度時(shí),表示居中 */
line-height: 5rem;
}

/* ..tt.active中間不加空格,表示這兩個(gè)名稱出現(xiàn)在同一個(gè)標(biāo)簽中*/
.tt.active,
.tt:hover {
background-color: rgb(231, 150, 28);
color: white;
}

.tab-content {
height: 250px;
width: 100%;
background-color: burlywood;
position: relative;

}

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

/* .tc子標(biāo)簽的第一個(gè) */
.tc.active{
display: block;
}
.tc:nth-of-type(1){
background-color: lightblue;

}

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

.tc:nth-of-type(3){
background-color: peachpuff;

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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