作者:louis | 来源:互联网 | 2022-06-27 16:32
本文实例讲述了纯Javascript实现的分页插件。分享给大家供大家参考。具体如下:
//总条数(必填)
var Num=Number(<&#63;php echo $count;&#63;>)
//当前页(必填)
var index = Number(<&#63;php echo $page;&#63;>);
/* //每页的条数(可选,默认每页10条) */
var pageNum=Number(10);
/* //最大显示的页码的数目(可选,默认显示5个页码,页码数目必须大于等于1) */
var maxPageNum=Number(5);
//以下可忽略
//计算得出总页数
var count = (Num%pageNum)>0&#63;(Num/pageNum+1):(Num/pageNum);
count=Math.floor(count);//取整转化为数据类型
//显示的最小页码,
var first=1;
//显示的最大页码,首先last<=count;其次last是小于等于count的最大数//last=index+maxPageNum/2;
var last =1;
var decrease=Math.floor(maxPageNum/2);//当前页向上增加值
var increase=Math.floor(maxPageNum/2);//当前页向下减少值
if(maxPageNum>=1){
if(maxPageNum==1){//最多显示一页时
first=index<=count&#63;index:count;
last=index<=count&#63;index:count;
}else{
//first要大于零
first=(index-decrease);
while(first<=0){
first++;
}
//first判断显示的最后一页
if((count-index)<=decrease){
var diff=count-first;
while(diff=1){
if(last<=count){
break;
}
last--;
}
//last//判断显示的最后一页与maxPageNum的关系
last=last>=maxPageNum&#63;last:(maxPageNum>count&#63;count:maxPageNum);
}
}else{
alert("至少需要显示一个页码!");
}
var prev = index - 1;//上一页
var next = index+ 1;//下一页
var str = "";
if(count==0){
str += "共0页 | ";
}else if(index>count||index<=0){
str=" | 页码超出范围 | ";
}else if (count > 0) {
str += "";
if(first>1){
str += " ... ";
}
var i=1;
for(i=first;i<=last; i++){
if(i==index){
str += " [" + i+ "] ";
}else{
str += " " + i+ " ";
}
}
if(last... ";
}
str+=" | 共"+ Num +"条 | ";
/* if(index!=1){
str +="上一页 | ";
}
if(index下一页";
}*/
if(index!=1&&count>1){
str += " 首页 | ";
}
if(index!=count&&count>1&&index 尾页 " ;
}
str+="
";
}
//分页区域填写
$('.page').html(str);
//根据页码查询,
function submit(pageIndex) {
//var sortInfo = $.getUrlParam('sortInfo');//判断是哪一个页面的查询
var url = "<&#63;php echo current_url();&#63;>&#63;page="+pageIndex+"&field=<&#63;php echo$field;&#63;>&value=<&#63;php echo $field_value;&#63;>";
window.location.href=url;
}
希望本文所述对大家的Javascript程序设计有所帮助。