관리 메뉴

moozi

accordian 본문

TIS_2016/HTML5_1기

accordian

moozi 2016. 4. 5. 14:32

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <title>accordian</title>

    <style type="text/css">

/*

    div.panel {

        padding: 0 18px;

        background-color: white;

        display: none;

    }

        

    div.panel.show { 

        display: block !important;

    }

*/

        

    div.panel {

        padding: 0 18px;

        background-color: white;

        max-height: 0;

        overflow: hidden;

        transition: 0.6s ease-in-out;

        opacity: 0;

    }


    div.panel.show {

        opacity: 1;

        max-height: 500px; 

    }    

        

    button.accordion {

        background-color: #eee;

        color: #444;

        cursor: pointer;

        padding: 18px;

        width: 100%;

        text-align: left;

        border: none;

        outline: none;

        transition: 0.4s;

    }  

     

    button.accordion.active,

    button.accordion:hover {

        background-color: #ddd;

    } 

    

    /* + 아이콘 추가하기 */    

    button.accordion:after {

        content: '\02795'; /* '+' 의 유니코드 값 */

        font-size: 13px;

        color: #777;

        float: right;

        margin-left: 5px;

    }


    button.accordion.active:after {

        content: "\2796"; /* '-'의 유니코드 값 */

    }

        

    </style>

    <script type="text/javascript">

    function accordian(){    

        /* accordian클래스를 찾아서 배열 생성 */

        var acc = document.getElementsByClassName("accordion");


        var i;

        var j;

        for (i = 0; i < acc.length; i++) {

            //accordian을 클릭하면 콜백함수 실행.

            acc[i].onclick = function(){

                //열려진 섹션을 먼저 닫음.

                for (j = 0; j < acc.length; j++) {

                    acc[j].classList.remove("active");

                    acc[j].nextElementSibling.classList.remove("show");

                }

                // active클래스가 적용되어 있으면 미적용.

                // 적용 안되어 있으면 적용.

                this.classList.toggle("active");

                // active가 적용된 곳에 show적용

                this.nextElementSibling.classList.toggle("show");

            }

        }

    }

    </script>

</head>

<body onLoad="accordian()">

    <button class="accordion">Section 1</button>

    <div class="panel">

    <p>Lorem ipsum...</p>

    </div>


    <button class="accordion">Section 2</button>

    <div class="panel">

    <p>Lorem ipsum...</p>

    </div>


    <button class="accordion">Section 3</button>

    <div class="panel">

    <p>Lorem ipsum...</p>

    </div>


</body>

</html>

'TIS_2016 > HTML5_1기' 카테고리의 다른 글

버튼예제  (0) 2016.04.06
tab  (0) 2016.04.05
달력 예제  (0) 2016.04.05
2차 포트폴리오  (13) 2016.04.05
d-day 구하기 javascript  (0) 2016.04.04
Comments