이미지 슬라이드 - 페이드 효과
<main>
<div class="slider-wrap">
<div class="slider-img">
<div class="slider" data-name="이미지1"><img src="../assets/img/bg08.jpg" alt="이미지1"></div>
<div class="slider" data-name="이미지2"><img src="../assets/img/bg09.jpg" alt="이미지2"></div>
<div class="slider" data-name="이미지3"><img src="../assets/img/bg10.jpg" alt="이미지3"></div>
<div class="slider" data-name="이미지4"><img src="../assets/img/bg11.jpg" alt="이미지4"></div>
<div class="slider" data-name="이미지5"><img src="../assets/img/bg12.jpg" alt="이미지5"></div>
</div>
</div>
</main>
.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 .3s;
}
.slider::before {
position: absolute;
left: 5px;
top: 5px;
background: rgba(0, 0, 0, .4);
color: #fff;
padding: 5px 10px 3px;
line-height: 1.3;
font-family: "NEXONLv1Gothic";
content: attr(data-name);
}
.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;
}
const sliderWrap = $('.slider-wrap');
const sliderImg = $('.slider-img');
const slider = $('.slider');
let currentIndex = 0; //현재 보이는 이미지(첫 번째 이미지)
let sliderCount = slider.length; //이미지 갯수
setInterval(function () {
let nextIndex = (currentIndex + 1) % sliderCount;
slider.eq(currentIndex).fadeOut(); //현재 보이는 이미지를 사라지게
slider.eq(nextIndex).fadeIn(); //다음 이미지를 나타나게
currentIndex = nextIndex;
}, 2000);