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

前端 未结 5 1076
滥情空心
滥情空心 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:17

    Try the following updated jsFiddle:

    var li = $("li");
    
    $(".prev").click(function() {
        var activeLI = $("li.active");
        var index = $(activeLI).index();
    
        $(activeLI).removeClass('active');
    
        if (index > 0) {
            $(activeLI).prev().addClass('active');
        } else {
            $(li).last().addClass('active');
        }
    });
    
    $(".next").click(function() {
        var activeLI = $("li.active");
        var index = $(activeLI).index() + 1;
    
        $(activeLI).removeClass('active');
    
        if (index < li.length) {
            $(activeLI).next().addClass('active');
        } else {
            $(li).first().addClass('active');
        }
    });
    

    This will loop around to last element active (if previous on first) and first element active (if next on last).

提交回复
热议问题