carousel / display: noneκ³Ό element.remove() λΉκ΅
carousel ꡬν
<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>
* {
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 μ€μ΅ λ μμ ν μ΅νλ€κ³ μκ°νλλ° μλμλ κ±° κ°λ€. λ€μ νλ² μ 리νλ μκ°μ κ°μ‘λ€. κ° μ»¨ν μ΄λλ€μ μμ΄ λ€λ₯Έ ν λ리λ₯Ό μΆκ°ν΄μ μ΄ν΄λ³΄λ κ² λμμ΄ λ§μ΄ λλ€.
- λ²νΌμ΄ λλ Έμ λ νμ¬ μ΄λ―Έμ§λ₯Ό 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();
- ν΄λΉ μμλ₯Ό μμ μμ νλ€
- μλ°μ€ν¬λ¦½νΈμμ μμλ₯Ό μ νν λ
μ νλμ§ μλλ€
carousel ꡬν μ΄μ΄μ
νλ‘κ·Έλλ¨Έμ€ μκ³ λ¦¬μ¦ λ¬Έμ νμ΄ & μ€λ΅ λΆμ