이미지 슬라이드 - 연속적으로 넘기기
<main>
<section id="sliderType01">
<div class="slider__wrap">
<div class="slider__img">
<div class="slider"><img src="img/image06.jpg" alt="이미지1"></div>
<div class="slider"><img src="img/image07.jpg" alt="이미지2"></div>
<div class="slider"><img src="img/image08.jpg" alt="이미지3"></div>
<div class="slider"><img src="img/image09.jpg" alt="이미지4"></div>
<div class="slider"><img src="img/image10.jpg" alt="이미지5"></div>
</div>
</div>
</section>
</main>
<!--//main-->
<!--info-->
<div class="info">
<div class="title">
<h1><a href="sliderEffect.html">Javascript SliderEffect01</a>
<p>이미지 슬라이드 - 페이드 효과</p>
</div>
<div class="number">
<ul>
<li class="active"><a href="sliderEffect01.html">1</a>
<li><a href="sliderEffect02.html">2</a></li>
<li><a href="sliderEffect03.html">3</a></li>
<li><a href="sliderEffect04.html">4</a></li>
<li><a href="sliderEffect05.html">5</a></li>
<li><a href="sliderEffect06.html">6</a></li>
<li><a href="sliderEffect07.html">7</a></li>
<li><a href="sliderEffect08.html">8</a></li>
</ul>
</div>
<div class="source">
<button class="source-btn">소스보기
<div id="modal">
<div class="modal-wrap">
<div class="modal-cont">
<div class="code-view">
<div class="view-header" aria-hidden="true">
<span></span>
<span></span>
<span></span>
</div>
<div class="view-title">
<ul>
<li class="active"><a href="#">HTML</a>
<li><a href="#">CSS</a>
<li><a href="#">Javascript</a>
<li><a href="#">jQuery</a>
</ul>
</div>
<div class="view-cont"> *4
</div>
/*slider*/
.slider__wrap {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
}
.slider__img { /*화면보이는 구간*/
width: 800px;
height: 480px;
overflow: hidden;
}
.slider__inner { /*이미지 움직이는 영역*/
display: flex;
flex-wrap: wrap;
width: 4800px; /*이미지 총 길이*/
transform: translateX(0);
}
.slider {
width: 800px;
position: relative;
}
.slider::before {
position: absolute;
left: 5px;
top: 5px;
background: rgba(0,0,0,0.4);
color: #fff;
padding: 5px 10px 3px;
}
.slider:nth-child(1):before {
content: '이미지1';
}
.slider:nth-child(2):before {
content: '이미지2';
}
.slider:nth-child(3):before {
content: '이미지3';
}
.slider:nth-child(4):before {
content: '이미지4';
}
.slider:nth-child(5):before {
content: '이미지5';
}
.slider:nth-child(6):before {
content: '이미지1';
}
//slider01
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 sliderCount = slider.length;
let sliderWidth = sliderImg.offsetWidth;
let sliderClone = sliderInner.firstElementChild.cloneNode(true);
sliderInner.appendChild(sliderClone);
function sliderEffect(){
currentIndex++;
sliderInner.style.transition = "all 0.6s";
sliderInner.style.transform = "translateX(-"+ sliderWidth * currentIndex +"px)";
//마지막 사진에 갔을 때
if(currentIndex == sliderCount){
setTimeout(() => {
// 이미지 초기화
sliderInner.style.transition = "0s";
sliderInner.style.transform = "translateX(0px)";
}, 300);
currentIndex =0;
}
}
setInterval(sliderEffect,2000);