I'm

Developer, Designer, Photographer

GET IN TOUCH

  • Buk-gu, Busan, Republic of Korea
  • ungdoli0916@naver.com
  • Kakao ID : Ungdoli
Your message has been sent. Thank you!
(즐거웅코드) 자바스크립트 누르면 펼쳐지는 아코디언 메뉴 구현하기

· 즐거'웅' 코드 (Source)/HTML & CSS & JAVASCRIPT
2021. 6. 16. 22:01

반응형

[즐거'웅'코드] 목차

  1. 예제 소스
  2. 구현 예시
예제 소스
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
<style>
    .accordion {
        background-color: #ffffff;
        cursor: pointer;
        padding: 16px;
        width: 100%;
        outline: none;
        border: none;
        text-align: left;
        font-size: 16px;
        transition: 0.4s;
    }
 
    .active, .accordion:hover {
        background-color: #f5f5f5;
        font-weight: bold;
    }
 
    .accordion:after {
        content: '\002B';
        font-weight: bold;
        float: right;
        margin-left: 5px;
    }
 
    .active:after {
        content: "\2212";
    }
 
    .panel {
        padding: 0 16px;
        background-color: #ffffff;
        max-height: 0;
        overflow: hidden;
        transition: max-height 0.2s ease-out;
        border-bottom: 1px solid #eeeeee;
    }
</style>
 
<button class="accordion">메뉴 1</button>
<div class="panel">
    <p>내용 1</p>
</div>
 
<button class="accordion">메뉴 2</button>
<div class="panel">
    <p>내용 2</p>
</div>
 
<button class="accordion">메뉴 3</button>
<div class="panel">
    <p>내용 3</p>
</div>
 
<script>
    var acc = document.getElementsByClassName("accordion");
    var i;
 
    for (i = 0; i < acc.length; i++) {
        acc[i].addEventListener("click"function () {
            this.classList.toggle("active");
            var panel = this.nextElementSibling;
            if (panel.style.maxHeight) {
                panel.style.maxHeight = null;
            } else {
                panel.style.maxHeight = panel.scrollHeight + "px";
            }
        });
    }
</script>
구현 예시
반응형