搜索:
首页 >> JavaScript >> Js高级编程 >> 仿豆瓣分页原型(Javascript版)

仿豆瓣分页原型(Javascript版)

2007-11-5 作者:tyhouse 来源:twocity.cn 投递文章

仿豆瓣分页原型(Javascript版)写了个分页的样式。

自我感觉,这样的分页前后兼顾,对于用户的体验是蛮好使的

Javascript分页代码如下:

<script language="javascript" type="text/javascript">
function QueryString(item){
 var sValue=location.search.match(new RegExp("[\?\&]"+item+"=([^\&]*)(\&?)","i"))
 return sValue?sValue[1]:sValue
}
var count = 560;
var perpage = 20;
var currentpage = QueryString("page");
if (currentpage==null){
 currentpage = 1;
}else{
 currentpage = parseInt(currentpage);
}
var pagecount = Math.floor(count/perpage);
var pagestr = "";
var breakpage = 9;
var currentposition = 4;
var breakspace = 2;
var maxspace = 4;
var prevnum = currentpage-currentposition;
var nextnum = currentpage+currentposition;
if(prevnum<1) prevnum = 1;
if(nextnum>pagecount) nextnum = pagecount;
pagestr += (currentpage==1)?'<span class="prev">< 前页</span>':'<span class="prev">< <a href="?page='+(currentpage-1)+'">前页</a></span>';
if(prevnum-breakspace>maxspace){
 for(i=1;i<=breakspace;i++)
  pagestr += '<a href="?page='+i+'">'+i+'</a>';
 pagestr += '<span class="break">...</span>';
 for(i=pagecount-breakpage+1;i<prevnum;i++)
  pagestr += '<a href="?page='+i+'">'+i+'</a>';
}else{
 for(i=1;i<prevnum;i++)
  pagestr += '<a href="?page='+i+'">'+i+'</a>';
}
for(i=prevnum;i<=nextnum;i++){
 pagestr += (currentpage==i)?'<span class="thispage">'+i+'</span>':'<a href="?page='+i+'">'+i+'</a>';
}
if(pagecount-breakspace-nextnum+1>maxspace){
 for(i=nextnum+1;i<=breakpage;i++)
  pagestr += '<a href="?page='+i+'">'+i+'</a>';
 pagestr += '<span class="break">...</span>';
 for(i=pagecount-breakspace+1;i<=pagecount;i++)
  pagestr += '<a href="?page='+i+'">'+i+'</a>';
}else{
 for(i=nextnum+1;i<=pagecount;i++)
  pagestr += '<a href="?page='+i+'">'+i+'</a>';
}
pagestr += (currentpage==pagecount)?'<span class="next">后页 ></span>':'<span class="next"><a href="?page='+(currentpage+1)+'">后页</a> ></span>';
document.getElementById("pagebar").innerHTML = pagestr;
</script>

[提示:你可先修改部分代码,再按运行]


 

 

Tags:分页  javascript 
相关文章
手机版 Js高级编程 Asp之家 Aspxhome.com
闽ICP备06017341号