仿豆瓣分页原型(Javascript版) 时间:2020-05-12 人气:0 好久没发过帖子了~~。 因为工作需要,仿豆瓣式写了个分页的样式。 自我感觉,这样的分页前后兼顾,对于用户的体验是蛮好使的。 仿豆瓣分页原型(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)?'< 前页':'< 前页'; if(prevnum-breakspace>maxspace){ for(i=1;i<=breakspace;i++) pagestr += ''+i+''; pagestr += '...'; for(i=pagecount-breakpage+1;i'+i+''; }else{ for(i=1;i'+i+''; } for(i=prevnum;i<=nextnum;i++){ pagestr += (currentpage==i)?''+i+'':''+i+''; } if(pagecount-breakspace-nextnum+1>maxspace){ for(i=nextnum+1;i<=breakpage;i++) pagestr += ''+i+''; pagestr += '...'; for(i=pagecount-breakspace+1;i<=pagecount;i++) pagestr += ''+i+''; }else{ for(i=nextnum+1;i<=pagecount;i++) pagestr += ''+i+''; } pagestr += (currentpage==pagecount)?'后页 >':'后页 >'; document.getElementById("pagebar").innerHTML = pagestr; </script> [Ctrl+A 全选 注:如需引入外部Js需刷新才能执行] 加载全部内容