본문 바로가기
DEV/JS30

[JS30] day 5 - Flex panels image gallery

by newjp 2021. 12. 9.

CSS, Flex 사용 부분

  1. 글씨 크기 변화 (scale)
  2. 클릭 하면 글씨 나타남 (tranform)
  3. 갤러리 배치 (flex)
    html {
      box-sizing: border-box;
      background: #000000;
      font-family: 'Noto Serif Display', serif;
      font-size: 20px;
      font-weight: 200;
    }
    
    body {
      margin: 0;
    }
    
    *, *:before, *:after {
      box-sizing: inherit;
    }

    .panels {
      min-height: 100vh;
      overflow: hidden;
      display: flex;
    }

    .panel {
      background: #6B0F9C;
      color: white;
      text-align: center;
      align-items: center;
      /* Safari transitionend event.propertyName === flex */
      /* Chrome + FF transitionend event.propertyName === flex-grow */
      transition:
        font-size 0.7s cubic-bezier(0.61,-0.19, 0.7,-0.11),
        flex 0.7s cubic-bezier(0.61,-0.19, 0.7,-0.11),
        background 0.2s;
      font-size: 20px;
      background-size: cover;
      background-position: center;
      flex: 1;
      display: flex;
      justify-content: center;
      align-items: center;
      flex-direction: column;
    }

    .panel1 { background-image:url(https://cdn.clien.net/web/api/file/F01/11677239/aabb782d97bb9b.jpg?w=780&h=30000); }
    .panel2 { background-image:url(https://cdn.clien.net/web/api/file/F01/11677240/aabb7c7066ba65.jpg?w=780&h=30000); }
    .panel3 { background-image:url(https://cdn.clien.net/web/api/file/F01/11677244/58f42b3636b27f.jpg?w=780&h=30000); }
    .panel4 { background-image:url(https://cdn.clien.net/web/api/file/F01/11677246/aabb87850b8393.jpg?w=780&h=30000); }

    /* Flex Children */
    .panel > * {
      margin: 0;
      width: 100%;
      transition: transform 0.5s;
      flex: 1 0 auto;
      display: flex;
      justify-content: center;
      align-items: center;
    }

    .panel > *:first-child { transform: translateY(-100%);}
    .panel.open-active > *:first-child { transform: translateY(0);}
    .panel > *:last-child { transform: translateY(100%);}
    .panel.open-active > *:last-child { transform: translateY(0);}

    .panel p {
      text-transform: uppercase;
      font-family: 'Noto Serif Display', serif;
      text-shadow: 0 0 4px rgba(0, 0, 0, 0.72), 0 0 14px rgba(0, 0, 0, 0.45);
      font-size: 1.5em;
    }
    
    .panel p:nth-child(2) {
      font-size: 3em;
    }

    .panel.open {
      flex: 5;
      font-size: 40px;
    }

JS 사용 부분

  1. 패널을 클릭하면 open된 설정을 가진 class를 toggle (갤러리 확장)
  2. 패널을 클릭하면 open-active class를 toggle (글씨 관련)
<script>
  const panels = document.querySelectorAll('.panel');
  function toggleOpen() {
    this.classList.toggle('open');
  }

  function toggleActive(e) {
    if(e.propertyName.includes('flex')) {
      this.classList.toggle('open-active');
    }
  }
  panels.forEach(panel => panel.addEventListener('click', toggleOpen));
  panels.forEach(panel => panel.addEventListener('transitionend', toggleActive));
</script>

'DEV > JS30' 카테고리의 다른 글

[JS30] day 7 - Array cardio day 2  (0) 2021.12.12
[JS30] day 6 - Ajax type ahead  (0) 2021.12.12
[JS30] day 4 - Array cadio day 1  (0) 2021.12.09
[JS30] day 3 - playing with CSS variables and JS  (0) 2021.12.07
[JS30] day 2 - CSS Clock  (0) 2021.12.06