[TIL] 210910

Lee SyongΒ·2021λ…„ 9μ›” 10일
0

TIL

λͺ©λ‘ 보기
23/204
post-thumbnail

πŸ“ 였늘 ν•œ 것

  1. carousel / display: noneκ³Ό element.remove() 비ꡐ

  2. carousel κ΅¬ν˜„


πŸ“– ν•™μŠ΅ 자료

  1. 사전 ν•™μŠ΅ κ°€μ΄λ“œ STEP 4 (carousel)

πŸ“š 배운 것

DOM


1. carousel(μΊλŸ¬μ…€)

(1) HTML μž‘μ„± β†’ λ’€μ—μ„œ μˆ˜μ •

    <section>
      <div class="title-image-box">
        <img src="/images/vanilla_coding_logo.png" />
      </div>
      <h1>Carousel</h1>

      <!-- Carousel Start -->
      <div class="carousel-outer">
        <div class="carousel-image-and-arrow">
          <button type="button" class="carousel-arrow left">
            <i class="fas fa-arrow-circle-left"></i>
          </button>
          <div class="carousel-image-box">
            <img src="/images/image-1.png" alt="image-1" class="carousel-image">
          </div>
          <button type="button" class="carousel-arrow right">
            <i class="fas fa-arrow-circle-right"></i>
          </button>
        </div>
        <div class="carousel-dots">
          <button class="carousel-dot">●</button>
          <button class="carousel-dot">●</button>
          <button class="carousel-dot">●</button>
          <button class="carousel-dot">●</button>
          <button class="carousel-dot">●</button>
        </div>
      </div>
      <!-- Carousel End -->
    </section>

(2) CSS μž‘μ„± β†’ λ’€μ—μ„œ μˆ˜μ •

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}
>
html {
  width: 100%;
}
>
body {
  background-image: url("./images/bg.jpeg");
  background-repeat: no-repeat;
  background-size: cover;
  font-family: "Varela Round", sans-serif;
}
>
section {
  margin: 60px auto;
  max-width: 1000px;
}
>
.title-image-box {
  text-align: center;
  padding: 0 80px;
}
>
.title-image-box img {
  width: 100%;
  max-width: 800px;
}
>
h1 {
  font-family: "Pacifico", cursive;
  text-align: center;
  font-size: 5vw;
}
>
.carousel-outer {
  display: flex;
  flex-direction: column;
  align-items: center;
  background-color: white;
  margin: 20px auto;
  width: 80%;
}
>
.carousel-image-and-arrow {
  display: flex;
  justify-content: center;
  align-items: center;
  margin: 0 auto;
  width: 100%;
  margin-top: 50px;
}
>
.carousel-arrow {
  background-color: transparent;
  border-style: none;
  width: 120px;
}
>
.fa-arrow-circle-left,
.fa-arrow-circle-right {
  font-size: 30px;
  cursor: pointer;
}
>
.carousel-image-box {
  width: 70%;
  margin: 0 auto;
}
>
.carousel-image {
  width: 100%;
  vertical-align: middle;
}
>
.carousel-dot {
  background-color: transparent;
  border-style: none;
  cursor: pointer;
  font-size: 20px;
  line-height: 2.5;
  margin: 0 15px;
  color: grey;
}

πŸ’­ HTML은 짧은 만큼 μ‰½κ²Œ μž‘μ„±ν–ˆλŠ”λ° CSSλŠ” 각 μ»¨ν…Œμ΄λ„ˆλ“€μ˜ width λΆ€λΆ„μ—μ„œ μ• λ₯Ό μ’€ λ¨Ήμ—ˆλ‹€. λΈŒλΌμš°μ € μ°½ 크기λ₯Ό 쀄일 λ•Œ HTML μ˜μ—­ λ°–μœΌλ‘œ 자꾸 μš”μ†Œλ“€μ΄ λΉ μ Έλ‚˜κ°€λŠ” κ±Έ ν•΄κ²°ν•˜λ €κ³  였래 고민을 ν–ˆλŠ”λ° HTML μ½”λ“œκ°€ 잘λͺ»λ˜μ–΄ μžˆμ—ˆλ‹€. λ‹€μŒλΆ€ν„° λ§‰νž 땐 μ§„μ§œ κΌ­ HTMLλΆ€ν„° 보기둜 λ‹€μ§ν–ˆλ‹€. 이 문제 κ΄€λ ¨ν•΄μ„œλ„ μ§€λ‚œλ²ˆ portfolio μ‹€μŠ΅ λ•Œ μ™„μ „νžˆ μ΅ν˜”λ‹€κ³  μƒκ°ν–ˆλŠ”λ° μ•„λ‹ˆμ—ˆλ˜ κ±° κ°™λ‹€. λ‹€μ‹œ ν•œλ²ˆ μ •λ¦¬ν•˜λŠ” μ‹œκ°„μ„ κ°€μ‘Œλ‹€. 각 μ»¨ν…Œμ΄λ„ˆλ“€μ— 색이 λ‹€λ₯Έ ν…Œλ‘λ¦¬λ₯Ό μΆ”κ°€ν•΄μ„œ μ‚΄νŽ΄λ³΄λŠ” 게 도움이 많이 됐닀.

(3) javascript μž‘μ„±

  • λ²„νŠΌμ΄ λˆŒλ Έμ„ λ•Œ ν˜„μž¬ 이미지λ₯Ό display: none μ²˜λ¦¬ν•˜κ³ , nextElementSiblingκ³Ό previousElementSibling에 style() λ©”μ„œλ“œλ₯Ό μ΄μš©ν•΄ display: inline을 λΆ€μ—¬ν•΄μ£Όλ©΄ 될 κ±° κ°™λ‹€κ³  μƒκ°ν–ˆμŒ
  • 일단 HTML에 2~5번째 이미지듀을 좔가함
<div class="carousel-image-box">
  <img src="/images/image-1.png" alt="image-1" class="carousel-image one">
  <img src="/images/image-2.png" alt="image-2" class="carousel-image two">
  <img src="/images/image-3.png" alt="image-3" class="carousel-image three">
  <img src="/images/image-4.png" alt="image-4" class="carousel-image four">
  <img src="/images/image-5.png" alt="image-5" class="carousel-image five">
</div>
  • κ·Έ ν›„ CSSμ—μ„œ 2~5번째 μ΄λ―Έμ§€λ“€μ—λŠ” display: none을 λΆ€μ—¬ν•΄ 처음 화면을 λ‘œλ”©ν•  λ•ŒλŠ” 첫 번째 μ΄λ―Έμ§€λ§Œ λ‚˜νƒ€λ‚˜λ„λ‘ ν•΄μ£Όμ—ˆμŒ
.two,
.three,
.four,
.five {
  display: none;
}
  • μžλ°”μŠ€ν¬λ¦½νŠΈμ— 이벀트λ₯Ό 등둝함
// 우츑 ν™”μ‚΄ν‘œ 클릭 μ‹œ λ‹€μŒ 이미지 보여주기
const rightArrow = document.querySelector('.right');

rightArrow.addEventListener('click', function goBack () {
  var presentImage = document.querySelector('.carousel-image');
  presentImage.style.display = 'none';
  presentImage.nextElementSibling.style.display = 'inline';
});

// 쒌츑 ν™”μ‚΄ν‘œ 클릭 μ‹œ 이전 이미지 보여주기
const leftArrow = document.querySelector('.left');

leftArrow.addEventListener('click', function goNext () {
  var presentImage = document.querySelector('.carousel-image');
  presentImage.style.display = 'none';
  presentImage.previousElementSibling.style.display = 'inline';
});
  • μ΄λ ‡κ²Œ μž‘μ„±ν•˜λ‹ˆκΉŒ 우츑 ν™”μ‚΄ν‘œλ₯Ό λˆ„λ₯΄λ©΄ λ‹€μŒ 이미지λ₯Ό 보여주긴 함
  • κ·ΈλŸ¬λ‚˜ ν•œ 번 더 λˆ„λ₯΄λ©΄ λ°˜μ‘μ΄ μ—†μŒ
  • μ™Όμͺ½ ν™”μ‚΄ν‘œλ₯Ό λˆ„λ₯΄λ©΄ console 창에 이런 였λ₯˜κ°€ 뜸
    TypeError: Cannot read properties of null (reading 'style') at HTMLButtonElement.goNext
  • κ·Έλ ‡λ‹€λ©΄ 우츑 λ²„νŠΌμ˜ 이전 ν˜•μ œ μš”μ†Œ(div)의 μžμ‹ imgλ₯Ό preventImage λ³€μˆ˜μ— ν• λ‹Ήν•œλ‹€λ©΄?
const rightArrow = document.querySelector('.right');

rightArrow.addEventListener('click', function goBack (event) {
  var presentImage = event.currentTarget.previousElementSibling.children[0];
  presentImage.style.display = 'none';
  presentImage.nextElementSibling.style.display = 'inline';
});
  • 처음만 될 뿐, 두 λ²ˆμ§ΈλΆ€ν„°λŠ” λˆŒλŸ¬λ„ λ°˜μ‘μ΄ μ—†λ‹€
  • ...λŠλ‚Œμƒ 였히렀 더 멀리 κ°„ κ±° κ°™μ•„μ„œ κ·Έλƒ₯ λ‹€μ‹œ λŒμ•„μ˜΄
  • μ΄λ²ˆμ—” ν˜„μž¬ 화면에 λ„μ›Œμ Έ μžˆλŠ” 이미지(presentImageλΌλŠ” λ³€μˆ˜)에 display: none을 μ„€μ •ν•΄μ£ΌλŠ” λŒ€μ‹ , μ•„μ˜ˆ κ·Έ 이미지λ₯Ό μ‚­μ œν•΄ λ΄„. 즉, presentImage.remove()
  • λ“œλ””μ–΄ 우츑 λ²„νŠΌμ„ λˆ„λ₯Ό λ•Œλ§ˆλ‹€ 'μ—°μ†μœΌλ‘œ' λ‹€μŒ μ΄λ―Έμ§€λ‘œ λ„˜μ–΄κ°€λ„λ‘ λ§Œλ“€κΈ° 성곡
// `우츑` ν™”μ‚΄ν‘œ 클릭 μ‹œ λ‹€μŒ 이미지 보여주기
const rightArrow = document.querySelector('.right');

rightArrow.addEventListener('click', function goBack () {
  var presentImage = document.querySelector('.carousel-image');
  presentImage.nextElementSibling.style.display = 'inline';
  presentImage.remove();
});

// 쒌츑 ν™”μ‚΄ν‘œ 클릭 μ‹œ 이전 이미지 보여주기
const leftArrow = document.querySelector('.left');

leftArrow.addEventListener('click', function goNext () {
  var presentImage = document.querySelector('.carousel-image');
  presentImage.previousElementSibling.style.display = 'inline';
  presentImage.remove();
});
- 
  • κ·ΈλŸ¬λ‚˜ ❗ 쒌츑 λ²„νŠΌμ„ λˆŒλ €μ„ λ•Œ 이전 μ΄λ―Έμ§€λ‘œ λ„˜μ–΄κ°€λŠ” 건 아직 μ•ˆλ¨

πŸ’‘ 비ꡐ !

  • display: none;

    • ν•΄λ‹Ή μš”μ†Œκ°€ 화면에 ν‘œμ‹œλ˜μ§€ μ•Šκ³ , κ·Έ μš”μ†Œμ—κ²Œ ν• λ‹Ήλœ 빈 곡간쑰차 ν™”λ©΄μ—μ„œ 사라진닀
    • μžλ°”μŠ€ν¬λ¦½νŠΈμ—μ„œ μš”μ†Œλ₯Ό 선택할 λ•Œ μ„ νƒλœλ‹€
  • element.remove();

    • ν•΄λ‹Ή μš”μ†Œλ₯Ό μ•„μ˜ˆ μ‚­μ œν•œλ‹€
    • μžλ°”μŠ€ν¬λ¦½νŠΈμ—μ„œ μš”μ†Œλ₯Ό 선택할 λ•Œ μ„ νƒλ˜μ§€ μ•ŠλŠ”λ‹€

✨ 내일 ν•  것

  1. carousel κ΅¬ν˜„ μ΄μ–΄μ„œ

  2. ν”„λ‘œκ·Έλž˜λ¨ΈμŠ€ μ•Œκ³ λ¦¬μ¦˜ 문제 풀이 & μ˜€λ‹΅ 뢄석

profile
λŠ₯λ™μ μœΌλ‘œ μ‚΄μž, ν–‰λ³΅ν•˜κ²ŒπŸ˜

0개의 λŒ“κΈ€