그럼에도 불구하고

👨‍💻

[Visual Studio Code] Accordion 만들기 본문

HTML, CSS/Function implementation

[Visual Studio Code] Accordion 만들기

zenghyun 2023. 2. 10. 10:20

 Accordion을 만들어보자

 

accordion.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="./accordion.css">
 
</head>
<body>
    <div class="area">
        <div class="accordion">
            <div class="accordion_item open">
                <button>
                    First accordion
                </button>
                <div class="accordion_content">
                    <ul>
                        <li>list 1</li>
                        <li>list 2</li>
                        <li>list 3</li>
                        <li>list 4</li>
                        <li>list 5</li>
                        <li>list 6</li>
                        <li>list 7</li>
                        <li>list 8</li>
                        <li>list 9</li>
                        <li>list 10</li>
                        <li>list 11</li>
                    </ul>
                </div>
            </div>
            <div class="accordion_item open">
                <button>
                    Second accordion
                </button>
                <div class="accordion_content">
                    <ul>
                        <li>list 1</li>
                        <li>list 2</li>
                    </ul>
                </div>
            </div>
        </div>
    </div>
    <script src="./accordion.js" defer></script>
</body>
</html>
cs

 

 

accodion.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
html {
    font-family: Impact, Haettenschweiler, 'Arial Narrow Bold', sans-serif;
}
.area {
    display: flex;
    height: 500px;
    width: 400px;
    border: 1px solid slateblue;
    background-color: #f0f0f0;
}
/* 아코디언 컴포넌트 동작 정의 */
.accordion {
    flex: 3 1 1px;
    display: flex;
    flex-direction: column;
}
/* 아코디언 아이템 닫혔을 때 */
.accordion .accordion_item {
    display: flex;
    min-height: fit-content;
    flex-direction: column;
    transition: all 0.5s;
}
.accordion .accordion_item>button {
    display: flex;
    padding: 1rem 1.25rem;
    border: none;
    align-items: center;
    font-family: Impact, Haettenschweiler, 'Arial Narrow Bold', sans-serif;
    border-bottom: 1px solid slateblue;
    overflow-anchor: none;
    background-color: rgb(255, 255, 255);
}
.accordion .accordion_item>button::after {
    flex-shrink: 0;
    width: 1.25rem;
    height: 1.25rem;
    margin-left: auto;
    content: '';
    background-image: url(./svg/arrow_black.svg);
    background-repeat: no-repeat;
    background-size: 1.25rem;
    transition: transform 0.2s ease-in-out;
}
.accordion .accordion_item>.accordion_content {
    display: none;
}
/* 아코디언 아이템 열렸을 때 */
.accordion .accordion_item.open {
    flex: 1 1 1px;
}
.accordion .accordion_item.open>button {
    border: 0;
    color: #413533;
    background-color: rgb(162, 152, 224);
}
.accordion .accordion_item.open>button::after {
    transform: rotate(-180deg);
}
.accordion .accordion_item.open>.accordion_content {
    display: flex;
    flex: 1 1 1px;
    overflow-y: auto;
}
.accordion .accordion_item>.accordion_content::-webkit-scrollbar {
    width: 5px;
    background-color: transparent;
}
.accordion .accordion_item>.accordion_content::-webkit-scrollbar-track {
    background-color: transparent;
}
.accordion .accordion_item>.accordion_content::-webkit-scrollbar-thumb {
    height: 17%;
    background-color: thistle;
    border-radius: 10px;
}
ul {
    display: flex;
    flex-direction: column;
    height: fit-content;
    width: 100%;
    padding: 0;
    margin: 0;
}
li {
    display: flex;
    align-items: center;
    height: 50px;
    padding-left: 10px;
    list-style: none;
    border-bottom: 1px solid slateblue;
    background-color: #ffffff;
}
cs

 

 

 

accordion.js

 

1
2
3
4
5
6
7
8
9
10
// 아코디언 클릭 이벤트 정의
const btns = document.querySelectorAll('.accordion .accordion_item > button');
 
btns.forEach(btn => {
    btn.addEventListener('click', (e) => {
        let accordion_item = e.currentTarget.parentNode;
        console.log("accordion_item", accordion_item);
        accordion_item.classList.toggle('open');
    });
});
cs

 

 

 

 

arrow_black.svg

※ 화살표 아이콘의 경우 이 소스 코드를 그대로 사용해도 되고, 다른 걸 사용해도 무방합니다 :) 

 

<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='#212529'>
<path fill-rule='evenodd' d='M1.646 4.646a.5.5 0 0 1 .708 0L8 10.293l5.646-5.
647a.5.5 0 0 1 .708.708l-6 6a.5.5 0 0 1-.708 0l-6-6a.5.5 00 1 0-.708z'/></svg>

 

 

 

See the Pen Untitled by zenghyun (@zenghyun) on CodePen.

 

 

 

REF: https://stickode.tistory.com/504

Comments