Jquery list show / hide 5 items onclick

◇◆丶佛笑我妖孽 提交于 2019-11-28 22:04:30

Here's an option: http://jsfiddle.net/JQq5n/

Don't know how/if you plan to show the previous items so I've left that out

This solution is shorter, and also works bidirectional (Previous and Next).
Fiddle: http://jsfiddle.net/JQq5n/61/

$('ul li:gt(4)').hide();

$('.prev').click(function() {
    var first = $('ul').children('li:visible:first');
    first.prevAll(':lt(5)').show();
    first.prev().nextAll().hide()
});

$('.next').click(function() {
    var last = $('ul').children('li:visible:last');
    last.nextAll(':lt(5)').show();
    last.next().prevAll().hide();
});

This is untested code, but I'm thinking it'll get you going in the right direction..

var currentShowingSet = 1;

$('#next').click(function() { 

    // Hide all.
    $('ul li').hide();

    // increment currently showing set of items.
    currentShowingSet++;

    // show the next 5 children of the list.
    for(var i = 1; i < 6; i++) {
        $('ul li:nth-child(' + (currentShowingSet * i) + ')').show();
    }
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!