How to create simple next and prev trigger button for slider's pagination buttons?

前端 未结 5 1072
滥情空心
滥情空心 2020-12-11 12:39

I am trying to carry a class between li\'s with combine each() and eq() methods when buttons clicked. I am using same code with previo

5条回答
  •  天命终不由人
    2020-12-11 13:07

    If for some reason you need the index, you could always do:

    $('.prev').on('click', function() {
        var i = $(".active").index();
            i--;
        $(".active").removeClass('active');
        $('li').eq(i).addClass('active');
    });
    
    $('.next').on('click', function() {
        var i = $(".active").index();
            i = i >= $('li').length-1 ? 0 : i+1;
        $(".active").removeClass('active');
        $('li').eq(i).addClass('active');
    });​
    

    FIDDLE

    If not :

    $('.prev, .next').on('click', function() {
        if ($(".active")[$(this).attr('class')]().index()!=-1)
        $(".active").removeClass('active')[$(this).attr('class')]().addClass('active');
    });
    

    FIDDLE

提交回复
热议问题