μΊλ¬μ (Carousel)
μΊλ¬μ (Carousel)μ μμ κ°μ΄ 컨ν μΈ λ₯Ό μ¬λΌμ΄λ ννλ‘ μννλ©° νμνλ UIλ₯Ό λ§νλ€.
μ΄λ 무ν 루ν κΈ°λ₯μ΄ κ°λ₯νκΈ° μν΄μλ μ²μμ μ΄λ―Έμ§ μ¬λΌμ΄λμμ μ΄μ λ²νΌμ ν΄λ¦ν μ λ§μ§λ§ μ¬λΌμ΄λλ‘ μ΄λμ νκ³
λ°λλ‘ λ§μ§λ§ μ¬λΌμ΄λμμ λ€μ λ²νΌμ ν΄λ¦ν μ μ²μ μ¬λΌμ΄λλ‘ μ΄λμ ν΄μΌ νλ€.
μ΄λ₯Ό μν΄ λ³Έλ μ€λΉλ μ΄ 4μ₯μ μ΄λ―Έμ§ μ΄μΈμ μ²μ λμ€λ μ΄λ―Έμ§μ λ§μ§λ§μ λμ€λ μ΄λ―Έμ§λ₯Ό κ°κ° 볡μ¬νμ¬ μλ£κ΅¬μ‘°μ μ λμ
μμΉμν¨λ€.
κ²°κ³Όμ μΌλ‘ μ¬λΌμ΄λ μ΄λ―Έμ§μ μ 보λ₯Ό λ΄κ³ μλ μλ£κ΅¬μ‘°μ 맨 μ²μ μ΄λ―Έμ§λ λ§μ§λ§ μ¬λΌμ΄λ μ΄λ―Έμ§μ΄κ³ λ§μ§λ§ μ΄λ―Έμ§λ μ²μ
μ¬λΌμ΄λ μ΄λ―Έμ§κ° μ μ¬λλ€.
$container.addEventListener('click', e => {
if(!e.target.classList.contains('carousel-control')) return;
if(e.target.classList.contains('next')) { //λ€μ λ²νΌμ λλ μ κ²½μ°
slides.style.setProperty('--currentSlide', ++currentSceneNum); //νμ¬ μ¬μ λ§κ² μ΄λ
slides.style.setProperty('--duration', 500);
if(currentSceneNum > 5) { //νμ¬ μ¬ μΈλ±μ€κ° λ§μ§λ§μΌ κ²½μ°
slides.style.setProperty('--currentSlide', 1);
slides.style.setProperty('--duration', 0);
currentSceneNum = 1; // νμ¬ μ¬ μΈλ±μ€λ₯Ό λ€μ μ²μ μ¬μ λμνλ μΈλ±μ€λ‘ μ΄κΈ°ν
}
} else { //μ΄μ λ²νΌμ λλ μ κ²½μ°
slides.style.setProperty('--currentSlide', (--currentSceneNum));
slides.style.setProperty('--duration', 500);
if(currentSceneNum < 0) { //νμ¬ μ¬ μΈλ±μ€κ° μ²μμΌ κ²½μ°
slides.style.setProperty('--currentSlide', 4); //λ§μ§λ§ μ¬μ λμνλ μΈλ±μ€λ‘ μ΄κΈ°ν
slides.style.setProperty('--duration', 0);
currentSceneNum = 4;
}
}
})
μ΄λ μΌλ°μ μΌλ‘ μλ€λ‘ μμ§μ΄λ μν©μ΄ μλ 맨 μ²μμ΄λ 맨 λ§μ§λ§μμ μ΄λν κ²½μ°μλ transitionμ duration κ°μ 0μΌλ‘ μ€μΌμ§
μ¬μ©μ μμ μμλ μ¬λΌμ΄λκ° μ΄λμ΄ λμλμ§ κ°μμ μΌλ‘ μΈμ§ λͺ»νκ² ν μ μλ€.
transitionend Event
무ν 루ν κΈ°λ₯μ ꡬννκΈ° μν΄μ Click μ΄λ²€νΈ λ΄μμ μ²λ¦¬ν΄μΌ λ 쑰건 μ°μ°λ€μ΄ λ무 λ§μμ Έμ μ½λμ ν¨μ¨μ±λ λΆμ‘±νκ³ κ°λ μ±λ λ¨μ΄μ‘λ€. μμ μ½λλ₯Ό λμ μ transitionend μ΄λ²€νΈλ₯Ό μ¬μ©νκ³ μ νλ€.
transitionendλ μ ν μνλ₯Ό μ ν μλ£λ‘, κΈ°λ³Έ λλ λΉ μ ν μνλ‘ μμ ν λλμ κ°λ€ - μ΄λ²€νΈκ° μλ°©ν₯μΌλ‘ λ°μν©λλ€.
$container.ontransitionend = () => {
isMoving = false;
// currentSlide === 0, μ¦ μ λμ μΆκ°ν ν΄λ‘ μ¬λΌμ΄λλ©΄ currentSlide += images.lengthλ‘ imageμ λ§μ§λ§(images.length)μΌλ‘ μ΄λ
// currentSlide === images.length + 1, μ¦ λ§μλ§μ μΆκ°ν ν΄λ‘ μ¬λΌμ΄λλ©΄ currentSlide -= images.lengthλ‘ imageμ μ λ(1)λ‘ μ΄λ
// ν΄λ‘ μ¬λΌμ΄λκ° μλλ©΄ currentSlide += 0μΌλ‘ μ΄λνμ§ μΊλλ€.
const delta = currentSlide === 0 ? 1 : currentSlide === images.length + 1 ? -1 : 0;
// ν΄λ‘ μ¬λΌμ΄λκ° μλλ©΄(delta === 0) μ΄λνμ§ μλλ€.
if (!delta) return;
currentSlide += images.length * delta;
move(currentSlide);
};
};