본문 바로가기

개인공부

javascript 클릭리스너 활용

반응형

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .stage {
            background-color: tomato;
            width: 100vw;
            height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            transition: 1s;
        }
        .click{
            background-color: skyblue;
        }

        .door {
            width: 100px;
            height: 150px;
            position: relative;
        }

        .door-back {
            overflow: hidden;
            width: 100%;
            height: 100%;
            left: 0;
            top: 0;
            position: absolute;
            background-color: black;
        }

        .door-body {
            position: absolute;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            transition: 0.5s;
            transform-origin: 0%;
        }

        .among {
            left: 0;
            bottom: 0;
            background-size: contain;
            width: 100px;
            height: 100px;
            background-repeat: no-repeat;
            background-position: 50% 100%;
            position: absolute;
            transform: translate3d(100%, 0, 0);
            transition: 0.5s 0.3s;
        }

        .door:nth-child(1) .among {
            background-image: url(images/1.png);
        }

        .door:nth-child(2) .among {
            background-image: url(images/2.png);
        }

        .door:nth-child(3) .among {
            background-image: url(images/3.png);
        }

        .door:nth-child(1) .door-body {
            background-color: rgba(255, 0, 0, 0.7);
        }

        .door:nth-child(2) .door-body {
            background-color: rgba(0, 255, 0, 0.7);
        }

        .door:nth-child(3) .door-body {
            background-color: rgba(0, 0, 255, 0.7);
        }

        .door-opend .door-body {
            transform: perspective(800px) rotateY(-110deg);
        }

        .door-opend .among {
            transform: translate3d(0, 0, 0);
        }
    </style>
</head>

<body>
    <div class="stage">
        <div class="door">
            <div class="door-back">
                <div class="among"></div>
            </div>
            <div class="door-body"></div>
        </div>
        <div class="door">
            <div class="door-back">
                <div class="among"></div>
            </div>
            <div class="door-body"></div>
        </div>
        <div class="door">
            <div class="door-back">
                <div class="among"></div>
            </div>
            <div class="door-body"></div>
        </div>
    </div>
    <script>
        (function () {

            const stageElem = document.querySelector('.stage');
            let currentTarget;

            function active(e) {
                e.classList.add('door-opend');
            }

            function inActive(e) {
                e.classList.remove('door-opend');
            }

            function doorHandler(e) {
                const targeElem = e.target;

                if (targeElem.classList.contains('door-body')) {
                    if (targeElem.parentNode.classList.contains('door-opend')) {
                        inActive(targeElem.parentNode);
                    } else {
                        active(targeElem.parentNode);
                    }
                    console.log(stageElem);
                    if(stageElem.classList.contains('click')){
                        stageElem.classList.remove('click');
                    }else{
                        stageElem.classList.add('click');
                    }

                }
            }
            stageElem.addEventListener('click', doorHandler);

        })();
    </script>
</body>

</html>
반응형

'개인공부' 카테고리의 다른 글

CSS : transitinoend 이벤트  (0) 2021.01.01
CSS : Scroll 사용  (0) 2021.01.01
이벤트 위임  (2) 2020.12.30
CSS : 움직임, 클릭리스너 적용  (2) 2020.12.30
CSS : relative 와 absolute 정복  (0) 2020.12.28