스크립트 질문 입니다.

스크립트 질문 입니다.

QA

스크립트 질문 입니다.

답변 2

본문

이전 다음 버튼 구현 중인데

순차적으로 되어야 되는데 먼가 중간 객체를 건너띄는 현상이 있는데 어디가 잘못된걸까요?

 

저 부분이 2가 되어야 되는데 이상하네요.

32936023_1714636362.6245.png

 


$('.tabBtn_next').click(function(){
        
        $(".newsWrapWrap .newsWrap").removeClass('active');
        if($(".tabItem > .item:nth-child(1)")) {
            $(".newsWrapWrap .newsWrap").removeClass('active');
            $(".newsWrapWrap > .newsWrap:nth-child(1)").addClass('active');
        }
        if($(".tabItem > .item:nth-child(2)")) {
            $(".newsWrapWrap .newsWrap").removeClass('active');
            $(".newsWrapWrap > .newsWrap:nth-child(2)").addClass('active');
        }
        if($(".tabItem > .item:nth-child(3)")) {
            $(".newsWrapWrap .newsWrap").removeClass('active');
            $(".newsWrapWrap > .newsWrap:nth-child(3)").addClass('active');
        }
        
        $('.tabItem').find('.active').next().addClass('active');
        $('.tabItem').find('.active').prev().removeClass('active');
        //console.log(tab_num);
    });
    $('.tabBtn_prev').click(function(){
        
        $(".newsWrapWrap .newsWrap").removeClass('active');
        if($(".tabItem > .item:nth-child(3)")) {
            $(".newsWrapWrap .newsWrap").removeClass('active');
            $(".newsWrapWrap > .newsWrap:nth-child(3)").addClass('active');
        }
        if($(".tabItem > .item:nth-child(2)")) {
            $(".newsWrapWrap .newsWrap").removeClass('active');
            $(".newsWrapWrap > .newsWrap:nth-child(2)").addClass('active');
        }
        if($(".tabItem > .item:nth-child(1)")) {
            $(".newsWrapWrap .newsWrap").removeClass('active');
            $(".newsWrapWrap > .newsWrap:nth-child(1)").addClass('active');
        }
        
        $('.tabItem').find('.active').prev().addClass('active');
        $('.tabItem').find('.active').next().removeClass('active');
        //console.log(tab_num);
    });

이 질문에 댓글 쓰기 :

답변 2


$('.tabBtn_next').click(function(){
    next++;
    if(next == $(".tabItem > .item").length){
        next = 0;
    }
    $(".newsWrapWrap .newsWrap").removeClass("active").eq(next).addClass("active");
})
let prev = 0;
$('.tabBtn_prev').click(function(){
    prev--;
    if(prev == -1){
        prev = $(".tabItem > .item").length-1;
    }
    $(".newsWrapWrap .newsWrap").removeClass("active").eq(prev).addClass("active");
})

 

전체 코드 및 예제 사이트가 (구조를 정확하게 몰라) 없어서 올려주신 코드 분석 해서 작성했습니다.

위 코드로는 100% 정확한 답변을 알수 없어요.

그냥 각 페이지별로 인덱스를 부여하셔서 작업하세요..

add 와 remove 조합이면 많이 조잡해보입니다.

답변을 작성하시기 전에 로그인 해주세요.
QA 내용 검색
질문등록
전체 124,182
© SIRSOFT
현재 페이지 제일 처음으로