๐ 2022. 12. 5. ์์์ผ
๐ Tutorial : ๋ฐ๋๋ผ ์๋ฐ์คํฌ๋ฆฝํธ๋ก ์ฌ๋ผ์ด๋ ๋ง๋ค๊ธฐ
const SHOWING_CLASS = "showing";
const firstSlide = document.querySelector(".slide:first-child");
function moveSlide() {
const currentSlide = document.querySelector(`.${SHOWING_CLASS}`);
if (currentSlide) {
currentSlide.classList.remove(SHOWING_CLASS);
const nextSlide = currentSlide.nextElementSibling;
if (nextSlide) {
nextSlide.classList.add(SHOWING_CLASS);
} else {
firstSlide.classList.add(SHOWING_CLASS);
}
} else {
firstSlide.classList.add(SHOWING_CLASS);
}
}
moveSlide();
setInterval(moveSlide, 2000);
๊ฐ ์ฌ๋ผ์ด๋๋ฅผ position: absolute
๋ก ์ง์ ํ์ฌ ํ ์ค์ ๊ฒน์น๋๋ก ํจ
์ฌ๋ผ์ด๋์ z-index๋ฅผ 0์ผ๋ก ํ๊ณ , showing
์ด๋ผ๋ ํด๋์ค๋ฅผ ๋ถ์์ ๋ z-index๋ฅผ 1๋ก ๋ฐ๊ฟ์ค
์๋ฐ์คํฌ๋ฆฝํธ๋ฅผ ํตํด showing ํด๋์ค๊ฐ ๋ถ์ด์๋ ์ฌ๋ผ์ด๋๋ฅผ ์ฐพ์๋ด๊ณ (์์ผ๋ฉด ์ฒซ๋ฒ์งธ ์ฌ๋ผ์ด๋์ showing ํด๋์ค ์ถ๊ฐํ๊ณ ์ข ๋ฃ)
๋ค์ ์ฌ๋ผ์ด๋๋ก ์ ํํ๊ธฐ ์ํด, ํ์ฌ showing ํด๋์ค๊ฐ ๋ถ์ด์๋ ์ฌ๋ผ์ด๋์์ showing ํด๋์ค๋ฅผ ์ ๊ฑฐํ๊ณ , ๊ทธ ๋ค์ ์ฌ๋ผ์ด๋(nextElementSibling
)์ showing ํด๋์ค๋ฅผ ๋ถ์ฌ์ค