简体   繁体   English

如何为滑块的分页按钮创建简单的下一个和上一个触发按钮?

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

I am trying to carry a class between li 's with combine each() and eq() methods when buttons clicked. 我试图通过单击按钮时在li之间使用结合each()eq()方法来携带一个类。 I am using same code with previous and next buttons except i+1 and i-1 but it is returning me different problems. 除了i+1i-1之外, i+1在上一个和下一个按钮上使用的是相同的代码,但它给我带来了不同的问题。

Here is jsFiddle to examine. 这是要检查的jsFiddle。

html: 的HTML:

<span class="prev">prev</span>
<ul>
    <li>0</li>
    <li>1</li>
    <li class="active">2</li>
    <li>3</li>
    <li>4</li>
</ul>
<span class="next">next</span>

jQuery: jQuery的:

var li = $('li');

$('.prev').click(function() {
    li.each(function(i) {
        if ( $(this).hasClass('active') ) {
            console.log(i);
            //this returning current true value = 2

            li.removeClass('active');
            li.eq(i-1).addClass('active');
            //this is working better
            //problem is: not selecting 4
        }
    });
});

$('.next').click(function() {
    li.each(function(i) {
        if ( $(this).hasClass('active') ) {
            console.log(i);
            //this returning current true value = 2

            //problem starts:
            //li.removeClass('active');
            //when this is active; all active classes are gone

            li.eq(i+1).addClass('active');
            //try to select next 'li' here
            //but it is selecting all li's bigger than current value
        }
    });
});

I'd try using something more like: 我会尝试使用更多类似的东西:

Here is working jsFiddle. 这是工作的jsFiddle。

$(".next").click(function(){
    if($("li.active").next().length > 0){
        $("li.active").removeClass("active").next("li").addClass("active");
    }
});
$(".prev").click(function(){
    if($("li.active").prev().length > 0){
        $("li.active").removeClass("active").prev("li").addClass("active");
    }
});​

using the 'next' selector: Jquery Next 使用“下一个”选择器: jQuery Next

I avoid math when possible. 我会尽可能避免数学运算。 Counting is hard :-). 很难计数:-)。 That said I'm thinking your problem above may be with the index. 就是说,我认为您上面的问题可能与索引有关。 Hard to say. 很难说。 I'd avoid using selectors like "li" when dealing with a list like this. 处理这样的列表时,我会避免使用“ li”之类的选择器。 Try putting a class on the 'ul' portion of it and addressing your li's as: ".myList li" 尝试在该类的“ ul”部分放置一个类,并将您的li命名为:“ .myList li”

You should return false; 您应该return false; after you find your first class of active : http://jsfiddle.net/ufomammut66/np4Pt/ 在您找到第一类activehttp : //jsfiddle.net/ufomammut66/np4Pt/

Whats happening is your iterating over the elements moving forward and changing each one in order. 发生的事情是您迭代前进的元素并按顺序更改每个元素。 So as you go through each case your setting the next one in sequence as active which in turn makes the next $(this).hasClass('active') check true. 因此,当您遍历每种情况时,将下一个依次设置为活动状态,这又会使下一个$(this).hasClass('active')检查为true。 This is why the prev event is working, we're moving in reverse order of the loop. 这就是为什么prev事件起作用的原因,我们以循环的相反顺序进行移动。 So we just need to stop iterating over our collection after we find our active case. 因此,我们只需要在找到活动案例后就停止对集合进行迭代。

Edit 1 编辑1

You can use .length on any selected object to get how many are returned. 您可以在任何选定对象上使用.length来获取返回的数量。 So in the example code you provided we can use li.length to get how many li elements are on the page. 因此,在您提供的示例代码中,我们可以使用li.length来获取页面上有多少li元素。 You may want to use a class selector just to ensure that other li elements are not being used to calculate your ceiling. 您可能想使用类选择器只是为了确保不使用其他li元素来计算您的上限。 I have updated the jsFiddle to reflect these changes. 我已经更新了jsFiddle以反映这些更改。

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 小提琴

You could try even this: 您甚至可以尝试:

$('.prev').click(function() {
    var el = $.find('li[class=active]'); 
    //check if are prev li
    if(!$(el).prev().is(':first')) $(el).removeClass('active').prev().addClass('active');
});

$('.next').click(function() {
    var el = $.find('li[class=active]'); 
    //check if are next li
    if(!$(el).next().is(':last')) $(el).removeClass('active').next().addClass('active');
});

Try the following updated jsFiddle : 尝试以下更新的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). 这将循环到最后一个元素处于活动状态(如果前一个在前)和第一个元素处于活动状态(如果下一个在后一个)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM