简单的jQuery分页

时间:2012-04-05 19:22:50

标签: javascript jquery html css pagination

我有一个大约有50个div的页面。我想将这些div组织成六个一组,以便客户端不会“信息过载”。我创建了一个简单example/reduced test case的问题。正如你所看到的,有很多div,我想要它,这样当页面加载时,只有前六个是可见的,但是当你点击第2页或下一页时,接下来的六个就会显示出来。您所在页码的类别也应设置为class="current"

到目前为止,我已经尝试过使用jQuery,但是我已经陷入困境了!任何帮助将不胜感激!

3 个答案:

答案 0 :(得分:21)

当请求页面时,隐藏所有内容div,然后遍历它们并显示应出现在“页面”上的内容:

showPage = function(page) {
    $(".content").hide();
    $(".content").each(function(n) {
        if (n >= pageSize * (page - 1) && n < pageSize * page)
            $(this).show();
    });        
}

http://jsfiddle.net/jfm9y/184/

答案 1 :(得分:5)

此代码的部分内容并不漂亮,但我认为它可以完成这项工作

var currentpage = 1;
var pagecount = 0;

function showpage(page) {
    $('.content').hide();
    $('.content').eq((page-1)*6).show().next().show().next().show().next().show().next().show().next().show();
    $('#pagin').find('a').removeClass('current').eq(page).addClass('current');

}

$("#pagin").on("click", "a", function(event){
    event.preventDefault();
    if($(this).html() == "next") {
        currentpage++;
    }
    else if($(this).html() == "prev") {
        currentpage--;
    } else {
            currentpage = $(this).html();
    }
    if(currentpage < 1) {currentpage = 1;}
    if(currentpage > pagecount) {currentpage = pagecount;}
    showpage(currentpage);
});                                                                  

$(document).ready(function() {
    pagecount = Math.floor(($('.content').size()) / 6);
    if (($('.content').size()) % 6 > 0) {
        pagecount++;
    }

    $('#pagin').html('<li><a>prev</a></li>');
    for (var i = 1; i <= pagecount; i++) {
        $('#pagin').append('<li><a class="current">' + i + '</a></li>');
    }
    $('#pagin').append('<li><a>next</a></li>');
    showpage(1);

});​

答案 2 :(得分:0)

以下代码摘自https://deltafrog.com/pagination-jquery-without-plugin/

jQuery('document').ready(function(){
var item_per_page=5;
var $block=jQuery('.block');
var block_count=$block.length;
var number_of_pages=Math.ceil(block_count/item_per_page);

//append pagination in body
jQuery('body').append("<div class='pagination'></div>");
for(var i=1; i<=number_of_pages; i++){
    jQuery('.pagination').append("<div class='page'>"+i+"</div>");  
}

//activate first page
jQuery(".page:first-child").addClass('active');
jQuery('.block').filter(function( index ) { return index < item_per_page;}).addClass('active');

//activate pagination on click
jQuery('body').delegate('.page','click',function(){
    var page_index=jQuery(this).index();
    var start=page_index*item_per_page;
    $block.removeClass('active');
    jQuery(".page").removeClass('active');
    jQuery(".page").eq(page_index).addClass('active');
    for(var j=0;j<item_per_page;j++){
        $block.eq(start+j).addClass('active');
    }

});

});
相关问题