๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
์ดํŽ™ํŠธ

03. ์Šฌ๋ผ์ด๋“œ ์ดํŽ™ํŠธ - ํŠธ๋žœ์ง€์…˜ ํšจ๊ณผ

by Youcodein 2022. 8. 29.
728x90
๋ฐ˜์‘ํ˜•

์Šฌ๋ผ์ด๋“œ ์ดํŽ™ํŠธ 01. ํŠธ๋žœ์ง€์…˜ ํšจ๊ณผ

javascript๋ฅผ ์ด์šฉํ•˜์—ฌ ์Šฌ๋ผ์ด๋“œ ์ดํŽ™ํŠธ๋ฅผ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค. ์—ด์‹ฌํžˆ ๊ณต๋ถ€ํ•˜์„ธ์š”!


์Šฌ๋ผ์ด๋“œ ์ดํŽ™ํŠธ 01

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ

const sliderWrap = document.querySelector(".slider__wrap");
const sliderImg = sliderWrap.querySelector(".slider__img");
const slider = sliderWrap.querySelectorAll(".slider");

let currentIndex = 0;   //ํ˜„์žฌ ๋ณด์ด๋Š” ์ด๋ฏธ์ง€ ๋ณ€์ˆ˜
let sliderCount = slider.length;    //์ด๋ฏธ์ง€ ๊ฐฏ์ˆ˜

setInterval(() => {

    let nextIndex = (currentIndex + 1) % sliderCount; //๋‹ค์Œ ์ด๋ฏธ์ง€

    slider[currentIndex].style.opacity = "0";  //์ฒซ๋ฒˆ์งธ ์ด๋ฏธ์ง€๋ฅผ ์•ˆ๋ณด์ด๊ฒŒ
    slider[nextIndex].style.opacity = "1";  //๋‘๋ฒˆ์งธ ์ด๋ฏธ์ง€๋ฅผ ๋ณด์ด๊ฒŒ

    currentIndex = nextIndex;


    //0 1 2 3 4 5 6 7 8 9...
    //0 1 2 3 4 0 1 2 3 4...


}, 2000);       
728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€