이미지 슬라이드 - 닷메뉴 추가
<main>
<div class="slider-wrap">
<div class="slider-img">
<div class="slider-inner">
<div class="slider" data-name="이미지1">
<img src="../assets/img/bg32.jpg" alt="이미지1">
</div>
<div class="slider" data-name="이미지2">
<img src="../assets/img/bg39.jpg" alt="이미지2">
</div>
<div class="slider" data-name="이미지3">
<img src="../assets/img/bg37.jpg" alt="이미지3">
</div>
<div class="slider" data-name="이미지4">
<img src="../assets/img/bg36.jpg" alt="이미지4">
</div>
<div class="slider" data-name="이미지5">
<img src="../assets/img/bg35.jpg" alt="이미지5">
</div>
</div>
</div>
<div class="slider-btn">
<a href="#" class="prev">prev</a>
<a href="#" class="next">next</a>
</div>
<div class="slider-dot">
<!-- <a href="#" class="dot active">1</a>
<a href="#" class="dot">2</a>
<a href="#" class="dot">3</a>
<a href="#" class="dot">4</a>
<a href="#" class="dot">5</a> -->
</div>
</div>
</main>
/* slider */
.slider-wrap {
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
}
.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: 4000px; */
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);
}
.slider-btn a {
position: absolute;
top: 50%;
transform: translateY(-50%);
z-index: 1000;
width: 50px;
height: 50px;
background: rgba(0,0,0,.4);
text-align: center;
line-height: 50px;
transition: all 0.2s;
display: block;
color: #fff;
text-decoration: none;
}
.slider-btn a:hover {
background: rgba(241, 96, 86, 1);
}
.slider-btn .prev {
left: 0;
}
.slider-btn .next {
right: 0;
}
.slider-dot {
position: absolute;
left: 50%;
transform: translateX(-50%);
bottom: 20px;
}
.slider-dot .dot {
width: 20px;
height: 20px;
background: rgba(0,0,0,.4);
display: inline-block;
border-radius: 50%;
transition: all .3s;
text-indent: -9999px;
margin: 3px;
}
.slider-dot .dot.active {
background: rgba(255,255,255,.9);
}
const sliderWrap = document.querySelector(".slider-wrap");
const sliderImg = sliderWrap.querySelector(".slider-img");
const sliderInner = sliderImg.querySelector(".slider-inner");
const slider = sliderInner.querySelectorAll(".slider");
const sliderBtn = sliderWrap.querySelector(".slider-btn");
const sliderBtnPrev = sliderBtn.querySelector(".prev");
const sliderBtnNext = sliderBtn.querySelector(".next");
const sliderDot = sliderWrap.querySelector(".slider-dot");
let sliderCount = slider.length;
let sliderWidth = document.querySelector(".slider").offsetWidth;
let sliderTotalWidth = sliderWidth * sliderCount;
let currentIndex = 0;
let dotIndex = "";
// 즉시 실행 함수
(() => {
// 기본 셋팅
sliderInner.style.width = sliderTotalWidth + "px";
sliderInner.style.transition = "all 300ms ease-in";
sliderInner.style.left = 0;
// 닷 메뉴 셋팅
slider.forEach((e, i) => {
let active = "";
if(i === currentIndex) active = "active";
dotIndex += `<a href='#' class='dot ${active}'>${i}</a>`;
})
sliderDot.innerHTML = dotIndex;
})()
// 이미지 움직이기
const gotoSlider = num => {
sliderInner.style.left = -sliderWidth * num + "px";
currentIndex = num;
dotActive()
}
// 왼쪽 버튼
const btnPrev = (e) => {
e.preventDefault();
let prevIndex = (currentIndex - 1) % sliderCount;
if(currentIndex == 0) prevIndex = sliderCount - 1;
gotoSlider(prevIndex);
}
// 오른쪽 버튼
const btnNext = (e) => {
e.preventDefault();
let NextIndex = (currentIndex + 1) % sliderCount;
gotoSlider(NextIndex);
}
// 닷 메뉴 활성화
// function dotActive(){
// sliderDot.querySelectorAll("a").forEach((el, i) => {
// if(el.classList.contains("active")){
// el.classList.remove("active");
// }
// });
// sliderDot.querySelectorAll("a")[currentIndex].classList.add("active");
// }
// 닷 메뉴 활성화 - 간단
function dotActive(){
sliderDot.querySelectorAll("a").forEach((el,index)=>{
index == currentIndex ? el.classList.add('active') : el.classList.remove('active');
});
}
// 닷 메뉴 버튼
sliderDot.querySelectorAll("a").forEach((dot, index) => {
dot.addEventListener("click", (e) => {
e.preventDefault();
gotoSlider(index);
dotActive();
});
});
sliderBtnPrev.addEventListener("click", btnPrev);
sliderBtnNext.addEventListener("click", btnNext);
// 소스보기 실행함수
modal();