이미지 슬라이드 - 닷메뉴 추가
<main>
<div class="slider-wrap">
<div class="slider-img">
<div class="slider-inner">
<div class="slider" data-name="이미지1">
<img src="../assets/img/bg22.jpg" alt="이미지1">
</div>
<div class="slider" data-name="이미지2">
<img src="../assets/img/bg24.jpg" alt="이미지2">
</div>
<div class="slider" data-name="이미지3">
<img src="../assets/img/bg26.jpg" alt="이미지3">
</div>
<div class="slider" data-name="이미지4">
<img src="../assets/img/bg28.jpg" alt="이미지4">
</div>
<div class="slider" data-name="이미지5">
<img src="../assets/img/bg21.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: 3200px; */
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 sliderInner = $(".slider-inner");
const slider = $(".slider");
const sliderBtn = $(".slider-btn");
const sliderDot = $(".slider-dot");
let sliderCount = slider.length;
let sliderWidth = slider.width();
let sliderTotalWidth = sliderWidth * sliderCount;
let currentIndex = 0;
let dotIndex = "";
// 이미지 가로값 셋팅
sliderInner.css({width: sliderTotalWidth});
// 닷 버튼 생성
// 이미지 갯수만큼 닷 메뉴 생성
slider.each(function(index){
dotIndex += "<a href='#' class='dot'>" + index + "</a>";
});
sliderDot.html(dotIndex);
sliderDot.find("a").eq(currentIndex).addClass("active");
// 이미지 움직이기
function gotoSlider(num){
sliderInner.animate({left: -sliderWidth * num}, 500, "easeInQuart");
currentIndex = num;
sliderDot.find("a").removeClass("active").eq(currentIndex).addClass("active");
};
// 닷 메뉴 클릭하기
sliderDot.on("click", "a", function(e){
e.preventDefault();
gotoSlider($(this).index());
});
// 버튼 클릭하기
// sliderBtn.click(function(){});
sliderBtn.on("click", "a", function(e){
e.preventDefault();
let nextIndex = (currentIndex + 1) % sliderCount;
let prevIndex = (currentIndex - 1) % sliderCount;
if(currentIndex == 0) prevIndex = sliderCount - 1;
if($(this).hasClass("prev")){
gotoSlider(prevIndex);
} else {
gotoSlider(nextIndex);
}
});
// 소스보기 실행함수
modal();