본문 바로가기

공부/TIL-D

toggle

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Toggle side nav</title>
  <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css">
  <style>
    html, body {
      height: 100%;
      margin: 0;
    }

    .container {
      position: relative;
      overflow-x: hidden; /* 가로 scroll bar 방지 */
      width: 100%;
      height: 100%;
    }

    .main, .side-nav {
      position: absolute;
      top: 0;
      height: 100%;
      transition: transform 0.8s;
    }

    .main {
      left: 0;
      width: 100%;
      background: antiquewhite;
    }

    .side-nav {
      left: -300px;
      width: 300px;
      background: rebeccapurple;
    }

    .active > .main,
    .active > .side-nav {
      transform: translate3d(300px, 0, 0);
    }

    .toggle {
      font-size: 2em;
      color: maroon;
      margin: 10px;
      cursor: pointer;
      transition: transform 0.5s;
    }

    .active .toggle {
      transform: rotate(180deg);
    }
  </style>
</head>
<body>
  <div class="container">
    <div class="side-nav"></div>
    <div class="main">
      <i class="toggle fas fa-arrow-circle-right"></i>
    </div>
  </div>
  <script>
    // 클래스 toggle이 눌리면, container에 자식들에게 active클래스를 추가한다
    // 버튼을 누를때마다 active가 있는지 없는지 검사해서
    // 없으면 붙히고 있으면 떨어뜨리고
    $toggle = document.querySelector('.toggle');

    $toggle.addEventListener('click', function(e){
      if(e.target.parentNode.parentNode.className == 'active'){
        e.target.parentNode.parentNode.className='container';
      }
      else{e.target.parentNode.parentNode.className='active'};
    });


  </script>
</body>
</html>
반응형

'공부 > TIL-D' 카테고리의 다른 글

stop-watch  (0) 2019.05.26
counter  (0) 2019.05.25
todo v2  (0) 2019.05.21
Todo v1  (0) 2019.05.20
디스트럭쳐링  (0) 2019.05.17