Javascript Slider Effect05

슬라이드 이펙트 - 이미지 슬라이드(버튼, 닷메뉴)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
이미지1
이미지2
이미지3
이미지4
이미지5
prev next
소스 보기
Javascript
HTML
CSS
const sliderWrap = document.querySelector(".slider__wrap");
const sliderImg = document.querySelector(".slider__img");       //보여지는 영역
const sliderInner = document.querySelector(".slider__inner");   //움직이는 영역
const slider = document.querySelectorAll(".slider");            //각각의 이미지
const sliderDot = document.querySelector(".slider__dot");   //닷 메뉴


let currentIndex = 0;   //현재 이미지의 인덱스
let sliderCount = slider.length;    //이미지 갯수
let sliderWidth = sliderImg.offsetWidth;    //이미지 가로값
let dotIndex = "";


function init(){
    slider.forEach(() => dotIndex += "<a href='#'' class='dot'>이미지1</a>");
    sliderDot.innerHTML = dotIndex;
    
    //첫번째 닷에만 active추가
    sliderDot.firstChild.classList.add("active");
}
init();

//이미지 이동
function gotoSlider(num){
    sliderInner.style.transition = "all 400ms";
    sliderInner.style.transform = "translateX("+ -sliderWidth*num +"px)";
    currentIndex = num;
    
    //두번째 이미지를 보고있으면 두번째 이미지에 클래스 추가
    // 01. 모든 닷메뉴의 active클래스 삭제
    // 02. 해당된느 닷메뉴에 active클래스 추가
    let sliderDotAcive = document.querySelectorAll(".slider__dot .dot");
    sliderDotAcive.forEach(el => el.classList.remove("active"));
    sliderDotAcive[num].classList.add("active");
}

//버튼 클릭했을 때
document.querySelectorAll(".slider__btn a").forEach((btn, index)=>{
    btn.addEventListener("click", ()=>{
        let preIndex = (currentIndex + (sliderCount - 1)) % sliderCount;
        let nextIndex = (currentIndex + 1) % sliderCount;

        if(btn.classList.contains("prev")){
            gotoSlider(preIndex);
        } else {
            gotoSlider(nextIndex);
        }
    });
});

//닷 버튼 클릭했을 때, 이미지 이동
document.querySelectorAll(".slider__dot .dot").forEach((dot, index)=>{
    dot.addEventListener("click", ()=>{
        gotoSlider(index);
    });
});

/* slider */
.slider__wrap {
    width: 100%;
    height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
}
.slider__img {      /* 이미지 보이는 영역 */
    position: relative;
    width: 800px;
    height: 450px;
    overflow: hidden;
}
.slider__inner {     /* 이미지를 감싸고 있는 부모 : 움직이는 부분 */
    display: flex;
    flex-wrap: wrap;
    width: 4800px;  /* 총 이미지 6개 */
}
.slider {           /* 개별적인 이미지 */
    position: relative;
    width: 800px;
    height: 450px;
}
.slider::before {
    position: absolute;
    left: 5px;
    top: 5px;
    background: rgba(0,0,0,0.4);
    color: #fff;
    padding: 5px 10px;
    border-radius: 5px;
}
.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';}

@media (max-width: 800px){
    .slider__img {
        width: 400px;
        height: 225px;
    }
}

/* slider__btn */
.slider__btn a {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    width: 50px;
    height: 50px;
    background: rgba(0,0,0,0.4);
    text-align: center;
    line-height: 50px;
    transition: all 0.2s;
    display: block;
    color: #fff;
    border-radius: 5px;
}
.slider__btn a:hover {
    background: rgb(251, 205, 2);
    color: rgb(235, 114, 0);
}
.slider__btn a.prev {
    left: 0;
}
.slider__btn a.next {
    right: 0;
}

/* slider__dot */
.slider__dot {
    position: absolute;
    left: 50%;
    transform: translateY(-50%);
    bottom: 20px;
}
.slider__dot .dot {
    width: 20px;
    height: 20px;
    background: rgba(0,0,0,0.4);
    display: inline-block;
    border-radius: 50%;
    text-indent: -9999px;
    transition: all 0.3s;
    margin: 2px;
}
.slider__dot .dot.active {
    background: rgba(255, 255, 255, 0.9);
}