Home  >  Article  >  Web Front-end  >  jquery动态分页效果堪比时光网_jquery

jquery动态分页效果堪比时光网_jquery

WBOY
WBOYOriginal
2016-05-16 16:35:261326browse

最近一直研究jquery的分页效果,刚刚弄好了一个,拿出来与大家分享。分页效果与时光网的差不多。

先在aspx页面放置一个

,这个是用来存放分页的。

然后建一个page.js文件,具体代码如下(js中用到的css类是自己设置的,这里就不给出了,具体的大家可以自己设置一下css样式):

复制代码 代码如下:

$(document).ready(function(){
var pageCount=0;//总页数,在数据处理的函数里设定

//////////////////////右部按钮分页显示
function right(pageCount,limit,rlimit){
var html="";
if(parseInt(pageCount)-limit>=rlimit){
for(var i=parseInt(pageCount)-rlimit+1; i html+=""+i+"";}
}
else{
for(var i=parseInt(limit)+1; i html+=""+i+"";}
}
return html;
}
//////////////////////////首页,尾页,上一页,下一页
function changeState(pageIndex,pageCount){
var $button1=$("div.pageDivs").find("#Button1");//上一页
var $button2=$("div.pageDivs").find("#Button2");//下一页
var $first=$("div.pageDivs").find("#First");//首页
var $last=$("div.pageDivs").find("#Last");//尾页
if(parseInt(pageIndex)==1){
$first.css("display","none");
$button1.css("display","none");}
else{
$first.css("display","inline");
$button1.css("display","inline");
$first.attr("page",1);
$button1.attr("page",parseInt(pageIndex)-1);}
if(parseInt(pageIndex)==pageCount){
$button2.css("display","none");
$last.css("display","none");}
else{
$last.css("display","inline");
$button2.css("display","inline");
$last.attr("page",pageCount);
$button2.attr("page",parseInt(pageIndex)+1);}

}
////////////////////////////////span动态分页 左边显示的页码个数,右边显示的页码个数,要求limit>rlimit
function span(pageCount,pageIndex,limit,rlimit){
var isContinue=true;//指示是否继续执行函数
var html="|";
var change=(parseInt(pageCount)-parseInt(rlimit))/(parseInt(limit)-2);//指示分页码可以变动的次数
if(pageCount!=0&&pageCount!=1){
if(pageCount for(var i=1; i html+="
"+i+""}
}
else{
if(parseInt(pageIndex) for(var i=1; i html+=""+i+"";}
html+="...";
html+=right(pageCount,limit,rlimit);
}
else{
if(parseInt(pageIndex)%(limit-2)==0){
if(parseInt(pageIndex)/(limit-2) for(var i=parseInt(pageIndex)-1; i html+=""+i+"";}
html+="...";
html+=right(pageCount,limit,rlimit);
}
else{
for(var i=1; i html+=""+i+"";}
html+="...";
var rest=parseInt(pageCount)-parseInt(rlimit);
if(rest for(var i=parseInt(rlimit)+1; i html+=""+i+"";}
}
else{
var start=parseInt(pageCount)-parseInt(limit)+1;
for(var i=start; i html+=""+i+"";}
}
}


}
else{
html=$("div.pageDivs").html();
$("div.pageDivs").html(html);
isContinue=false;
}
}

}
}
if(isContinue){
html+=">>|";
$("div.pageDivs").html(html);}
changeState(pageIndex,pageCount);
$("div.pageDivs").find("a[page=" + parseInt(pageIndex) + "]:visible").removeAttr("href").removeClass("disabled").addClass("current").siblings("a[page:visible").removeClass("current").addClass("disabled").attr("href", "#");
}

function page(pageIndex){

/////////////这里放你具体的数据显示,使用ajax动态加载处理数据

pageCount="通过数据处理获得的页面总数";

span(pageCount,pageIndex,7,2);//对分页效果进行调用,这里设置左边显示7个页码,右边显示2个页码。

}

//////////////////////////////为页码绑定事件

$("div.pageDivs").find("a:visible").live("click",function(){
var result=$(this).attr("page");
if((typeof $(this).attr("leaf"))!= 'undefined'){
$(this).removeAttr("href").removeClass("disabled").addClass("current").siblings().removeClass("current").addClass("disabled").attr("href","#");}

page(result);
});
});


这样就行了,以上分页的算法是可以封装的,与具体的项目没关系,可以通用。
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn