이미지 슬라이드 - 좌로 움직이기(연속적으로)
<!-- contents -->
<main>
<div class="slider-wrap">
<div class="slider-img">
<div class="slider-inner">
<div class="slider" data-name="이미지1">
<img src="../assets/img/bg6.jpg" alt="이미지1">
</div>
<div class="slider" data-name="이미지2">
<img src="../assets/img/bg7.jpg" alt="이미지2">
</div>
<div class="slider" data-name="이미지3">
<img src="../assets/img/bg8.jpg" alt="이미지3">
</div>
<div class="slider" data-name="이미지4">
<img src="../assets/img/bg9.jpg" alt="이미지4">
</div>
<div class="slider" data-name="이미지5">
<img src="../assets/img/bg1.jpg" alt="이미지5">
</div>
</div>
</div>
</div>
</main>
<!-- //contents -->
/* slider */
.slider-wrap {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
}
.slider-img {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
width: 800px;
height: 480px;
overflow: hidden;
}
.slider-img img {
width: 100%;
}
.slider-inner {
position: relative;
width: 4800px;
height: 480px;
overflow: hidden;
}
.slider-inner .slider {
float: left;
width: 800px;
height: 480px;
position: relative;
}
.slider-inner .slider::before {
position: absolute;
left: 5px;
top: 5px;
background: rgba(0,0,0,.4);
color: #fff;
padding: 5px 10px 3px;
font-family: 'NEXONLv1Gothic';
}
.slider-inner .slider:nth-child(1) {
z-index: 5;
}
.slider-inner .slider:nth-child(2) {
z-index: 4;
}
.slider-inner .slider:nth-child(3) {
z-index: 3;
}
.slider-inner .slider:nth-child(4) {
z-index: 2;
}
.slider-inner .slider:nth-child(5) {
z-index: 1;
}
.slider-inner .slider::before {
content: attr(data-name);
}
const sliderWrap = document.querySelector(".slider-wrap");
const sliderImg = document.querySelector(".slider-img");
const sliderInner = document.querySelector(".slider-inner");
const slider = document.querySelectorAll(".slider");
let currentIndex = 0;
let sliderWidth = document.querySelector(".slider").offsetWidth;
let sliderCount = slider.length;
let sliderClone = sliderInner.firstElementChild.cloneNode(true);
sliderInner.appendChild(sliderClone);
setInterval(() => {
currentIndex++; // 0 1 2 3 4 5 6 7 8 9
sliderInner.style.transition = "all 0.3s";
sliderInner.style.transform = "translate(" + (-sliderWidth * currentIndex) + "px)";
if(currentIndex == sliderCount){
setTimeout(() => {
sliderInner.style.transition = "0s";
sliderInner.style.transform = "translateX(0)";
}, 300);
currentIndex = 0;
};
}, 3000);
// 소스보기 실행함수
modal();