[FE - ConnecTo] DAY63 TILπŸ‘©πŸ»β€πŸ’»

JUNYΒ·2022λ…„ 10μ›” 12일
0

πŸ“šZeroBase ConnecTo Front-End

λͺ©λ‘ 보기
50/53
post-thumbnail

22.10.12 μˆ˜μ—… μ‹œκ°„ 쀑 κ³΅λΆ€ν•œ λ‚΄μš©λ“€μ„ μ •λ¦¬ν•˜μ˜€μŠ΅λ‹ˆλ‹€ 😊
ν”Όλ“œλ°±μ€ μ–Έμ œλ‚˜ ν™˜μ˜μž…λ‹ˆλ‹€! 🍊

πŸƒΒ Matching Cards κ΅¬ν˜„

  • μΉ΄λ“œ 뒀집기 animation κ΅¬ν˜„
    • κΈ°μ‘΄ κ΅¬μ‘°μ—μ„œ 뒀집기 κΈ°λŠ₯ κ΅¬ν˜„μ„ μœ„ν•΄μ„œ html ꡬ쑰λ₯Ό λ³€ν˜•ν•˜μ˜€μŒ.
      // λ³€κ²½ μ „
      <div class="cards" data-id="1">?</div>
      
      // λ³€κ²½ 이후
      <div class="cards" data-id="1">
        <div class="card-inner">
          <div class="card-front">?</div>
          <div class="card-back"></div>
        </div>
      </div>
    • transform: rotateλ₯Ό μ΄μš©ν•΄μ„œ μΉ΄λ“œ 뒀집기 κΈ°λŠ₯을 κ΅¬ν˜„ν•¨.
      .card-inner {
        position: relative;
        width: 100%;
        height: 100%;
        text-align: center;
        transition: transform 0.5s;
        transform-style: preserve-3d;
      }
      
      .cards.opened .card-inner {
        transform: rotateY(180deg);
      }
      
      .card-front,
      .card-back {
        ...
        backface-visibility: hidden;
      }
      
      ...
      
      .card-back {
      	...
        transform: rotateY(180deg);
      }

πŸ’‘ μΆ”κ°€ 지식
- transform-style
μš”μ†Œμ˜ μžμ‹μ΄ 3D 곡간에 λ°°μΉ˜ν•˜λ„λ‘ ν•˜λŠ” 속성

- backface-visibility
μš”μ†Œμ˜ λ’·μͺ½μ—μ„œ μ•žλ©΄μ΄ 보이게 ν•  지 말 지 κ²°μ •ν•˜λŠ” 속성
- κΈ°λ³Έκ°’ : visible
- 상속 : No
- μ• λ‹ˆλ©”μ΄μ…˜ : NO

  • ν΄λ¦­ν–ˆμ„ λ•Œ, 뒀집을 수 μžˆλ„λ‘ κ΅¬ν˜„

    • click 이벀트λ₯Ό μ‚¬μš©ν•΄μ„œ, opened classλ₯Ό ν† κΈ€μ‹œμΌœμ£Όμ—ˆμŠ΅λ‹ˆλ‹€.

  • μΉ΄λ“œ 2μž₯이 1쌍으둜 μΌμΉ˜ν•˜λŠ”μ§€ μ—¬λΆ€ 확인 둜직 κ΅¬ν˜„

    • μΉ΄λ“œ λ’€μ§‘μ—ˆμ„ λ•Œ, μΌμΉ˜ν•˜μ§€ μ•ŠλŠ” μΉ΄λ“œλ₯Ό λ‹€μ‹œ 뒀집을 λ•Œ, setTimeout의 μ‹œκ°„μ΄ λλ‚˜μ§€ μ•Šμ•˜μ„ λ•Œ, λ‹€λ₯Έ μΉ΄λ“œλ₯Ό λˆ„λ₯΄λ©΄ μΉ΄λ“œκ°€ μΌμΉ˜ν•˜μ§€ μ•Šλ”λΌλ„ 2개 이상이 μ—΄λ¦¬λŠ” λ¬Έμ œκ°€ λ°œμƒν•¨.

      // 이전
      let openCard = [];
      const completedCard = [];
      
      document.querySelector('.card-container').addEventListener('click', e => {
        if (
          !e.target.closest('.cards') ||
          e.target.closest('.cards').classList.contains('opened')
        )
          return;
        e.target.closest('.cards').classList.toggle('opened');
      
        openCard.push(e.target.nextElementSibling);
      
        if (openCard.length === 2) {
          if (openCard[0].textContent === openCard[1].textContent) {
            console.log('HOLYMOLY');
            completedCard.push(openCard[0]);
            completedCard.push(openCard[1]);
            openCard = [];
          } else {
            console.log('TRY AGAIN');
            setTimeout(() => {
              openCard[0].closest('.cards').classList.remove('opened');
              openCard[1].closest('.cards').classList.remove('opened');
              openCard = [];
            }, 500);
          }
        }
      });
      
      // 이후
      ...
      document.querySelector('.card-container').addEventListener('click', e => {
        if (
          !e.target.closest('.cards') ||
          e.target.closest('.cards').classList.contains('opened') ||
          openCard.length === 2
        )
          return;
        e.target.closest('.cards').classList.toggle('opened');
      
        ...
      });

      이전에 μΉ΄λ“œκ°€ 2개 이상 ν΄λ¦­λ˜μ—ˆμ„ λ•Œ, setTimeout의 μ‹œκ°„μ΄ λλ‚˜κΈ° 전에 λ‹€λ₯Έ μΉ΄λ“œλ₯Ό ν΄λ¦­ν•˜λ©΄, 배열이 μ΄ˆκΈ°ν™”λ˜κΈ° 전에 openCard 배열에 λ‹€λ₯Έ μš”μ†Œκ°€ μΆ”κ°€λ˜κΈ° λ•Œλ¬Έμ— μƒκΈ°λŠ” λ¬Έμ œμž„.

      κ·Έλž˜μ„œ, μ• μ΄ˆμ— openCard λ°°μ—΄μ˜ 길이λ₯Ό 2κ°€ λ„˜μ–΄κ°€μ§€ μ•Šλ„λ‘, λ°°μ—΄μ˜ 길이가 2일 λ•Œ, early return μ‹œμΌœμ£Όμ–΄μ„œ ν•΄κ²°ν•˜μ˜€μŒ.

  • 랜덀으둜 이미지 / κΈ€μž 뿌렀주기

    πŸ”€ ν”Όμ…” 예이츠 μ…”ν”Œ(Fisher-Yates Shuffle) μ•Œκ³ λ¦¬μ¦˜ 이용

    function shuffle(array) {
      const copyArray = [...array];
      for (let index = copyArray.length - 1; index > 0; index--) {
        const randomPosition = Math.floor(Math.random() * (index + 1));
    	[copyArray[index], copyArray[randomPosition]] = [copyArray[randomPosition], copyArray[index]];
      }
      return copyArray;
    }
  • reset λ²„νŠΌμ„ λˆ„λ₯΄λ©΄ μ΄ˆκΈ°ν™”
    • componentκ°€ μ•„λ‹Œ λ”°λ‘œ μ‹€ν—˜ν•΄λ³Έ νŒŒμΌμ—μ„œλŠ” reset λ²„νŠΌ λˆŒλ €μ„ λ•Œ, replayκ°€ μ•ˆλ¨. CBD ꡬ쑰둜 μ½”λ“œλ₯Ό λ³€κ²½ν•˜μ˜€μ„ λ•Œ 정상 μž‘λ™μ„ 함. 그런데, reset λˆŒλ €μ„ λ•Œ, transition이 λ™μž‘ν•˜λ©΄μ„œ 바뀐 μΉ΄λ“œκ°€ 살짝 λ³΄μ΄λŠ” issueκ°€ 있음
profile
μ„±μž₯ν•˜λŠ” 개발자🌼

0개의 λŒ“κΈ€