2024-06-03 使用 flex 下拉式切版 及 學習巢狀 CSS

2024-06-03 使用 flex 下拉式切版 及 學習巢狀 CSS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JavaScript 系列三:練習6 ── dropdown 下拉式選單</title>
<link rel="stylesheet" href="./reset.css">
<link rel="stylesheet" href="./06_js_dropdown.css">
</head>

<body>

<nav>
<ul class="main_nav">
<li>
<a href="#">首頁</a>
</li>
<li>
<a href="#">全部</a>
</li>
<li>
<a href="#">全部品牌</a>
</li>
<li class="about_me">
<a href="#">關於我們</a>
<ul>
<li><a href="#">企業宗旨</a></li>
<li><a href="#">團隊介紹</a></li>
<li><a href="#">創業故事</a></li>
</ul>
</li>
</ul>

</nav>



<script src="./06_js_dropdown.js"></script>
</body>

</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
nav{
.main_nav{
display: flex;
justify-content: space-around;

li{
position: relative;
}
ul{
position: absolute;
display: none;
width: fit-content;
flex-direction: column;
top: 100%;
right: 0;


}

.about_me.open_list ul{
display: flex;
background-color: black;
}

}
}

a{
text-decoration: none;
line-height: 1.5;
}
1
2
3
4
5
6
7
const elem_about_list = document.querySelector('.about_me');
function open_list(){

elem_about_list.classList.toggle('open_list');
}

elem_about_list.onclick = open_list;