이미지 슬라이드 - 페이드 효과
<!-- contents -->
<main>
<div class="slider-wrap">
<div class="slider-img">
<div class="slider" data-name="이미지1">
<img src="../assets/img/bg1.jpg" alt="이미지1">
</div>
<div class="slider" data-name="이미지2">
<img src="../assets/img/bg2.jpg" alt="이미지2">
</div>
<div class="slider" data-name="이미지3">
<img src="../assets/img/bg3.jpg" alt="이미지3">
</div>
<div class="slider" data-name="이미지4">
<img src="../assets/img/bg4.jpg" alt="이미지4">
</div>
<div class="slider" data-name="이미지5">
<img src="../assets/img/bg5.jpg" alt="이미지5">
</div>
</div>
</div>
</main>
<!-- //contents -->
/* slider */
.slider-wrap {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
}
.slider-img {
position: relative;
width: 800px;
height: 480px;
}
.slider-img img {
width: 100%;
}
.slider {
position: absolute;
left: 0;
top: 0;
transition: all 0.3s;
}
.slider::before {
position: absolute;
left: 5px;
top: 5px;
background: rgba(255, 255, 255, 0.4);
color: #fff;
padding: 5px 10px 3px;
font-family: 'NEXONLv1Gothic';
}
.slider:nth-child(1) {
z-index: 5;
}
.slider:nth-child(2) {
z-index: 4;
}
.slider:nth-child(3) {
z-index: 3;
}
.slider:nth-child(4) {
z-index: 2;
}
.slider:nth-child(5) {
z-index: 1;
}
.slider::before {
content: attr(data-name);
}
const sliderWrap = document.querySelector(".slider-wrap");
const sliderImg = document.querySelector(".slider-img");
const slider = document.querySelectorAll(".slider");
let currentIndex = 0 ; // 현재 보이는 이미지(첫번째 이미지)
let sliderCount = slider.length; // 이미지 갯수 구하기
setInterval(() => {
let nextIndex = (currentIndex + 1) % sliderCount;
// slider[currentIndex].style.display = "none";
// slider[nextIndex].style.display = "block";
slider[currentIndex].style.opacity = "0";
slider[nextIndex].style.opacity = "1";
currentIndex = nextIndex;
}, 2000);