分頁功能在做項目的過程中是常常用到的,下面是我常用的一款分頁效果:
1、分頁的CSS樣式(page.css)
1 #setpage { 2 margin: 15px auto; 3 text-align: center; 4 } 5 #setpage a:link,#setpage a:visited,#setpage a:hover,#setpage .current,#setpage #info{ 6 border:1px solid #DDD; 7 background:#0d6cbf; 8 display:inline-block; 9 margin:1px; 10 text-decoration:none; 11 text-align:center; 12 color:#fff; 13 padding: 5px 10px; 14 font-size: 16px; 15 border-radius: 5px; 16 } 17 #setpage a:hover{ 18 border:1px solid #0d6cbf; 19 background: #0d6cbf; 20 } 21 #setpage .current{ 22 border:1px solid #0d6cbf; 23 background: #fff; 24 margin:1px; 25 color: #000; 26 text-decoration: underline; 27 } 28 #setpage #info{ 29 width:auto; 30 }
2、分頁的js代碼(page.js)
1 var totalpage,pagesize,cpage,count,curcount,outstr; 2 //初始化 3 cpage = 1; //當前頁(設置為全局變量) 4 totalpage = 56; //總頁數 5 pagesize = 10; //每頁顯示的數據數量 6 outstr = ""; //顯示分頁效果 7 8 function gotopage(target) 9 { 10 cpage = target; //把頁面計數定位到第幾頁 11 12 //這里寫頁面跳轉時要調用的方法 13 var state = $("#state").find("option:selected").val(); 14 getData(state,cpage ); 15 16 //setpage(); 17 //reloadpage(target); //調用顯示頁面函數顯示第幾頁,這個功能是用在頁面內容用ajax載入的情況 18 } 19 20 function setpage() 21 { 22 if(totalpage<=10){ //總頁數小於十頁 23 for (count=1;count<=totalpage;count++) 24 { if(count!=cpage) 25 { 26 outstr = outstr + "<a href='javascript:void(0)' onclick='gotopage("+count+")'>"+count+"</a>"; 27 }else{ 28 outstr = outstr + "<span class='current' >"+count+"</span>"; 29 } 30 } 31 } 32 if(totalpage>10){ //總頁數大於十頁 33 if(parseInt((cpage-1)/10) == 0) 34 { 35 for (count=1;count<=10;count++) 36 { if(count!=cpage) 37 { 38 outstr = outstr + "<a href='javascript:void(0)' onclick='gotopage("+count+")'>"+count+"</a>"; 39 }else{ 40 outstr = outstr + "<span class='current'>"+count+"</span>"; 41 } 42 } 43 outstr = outstr + "<a href='javascript:void(0)' onclick='gotopage("+count+")'> 下一組 </a>"; 44 } 45 else if(parseInt((cpage-1)/10) == parseInt(totalpage/10)) 46 { 47 outstr = outstr + "<a href='javascript:void(0)' onclick='gotopage("+(parseInt((cpage-1)/10)*10)+")'>上一組</a>"; 48 for (count=parseInt(totalpage/10)*10+1;count<=totalpage;count++) 49 { if(count!=cpage) 50 { 51 outstr = outstr + "<a href='javascript:void(0)' onclick='gotopage("+count+")'>"+count+"</a>"; 52 }else{ 53 outstr = outstr + "<span class='current'>"+count+"</span>"; 54 } 55 } 56 } 57 else 58 { 59 outstr = outstr + "<a href='javascript:void(0)' onclick='gotopage("+(parseInt((cpage-1)/10)*10)+")'>上一組</a>"; 60 for (count=parseInt((cpage-1)/10)*10+1;count<=parseInt((cpage-1)/10)*10+10;count++) 61 { 62 if(count!=cpage) 63 { 64 outstr = outstr + "<a href='javascript:void(0)' onclick='gotopage("+count+")'>"+count+"</a>"; 65 }else{ 66 outstr = outstr + "<span class='current'>"+count+"</span>"; 67 } 68 } 69 outstr = outstr + "<a href='javascript:void(0)' onclick='gotopage("+count+")'> 下一組 </a>"; 70 } 71 } 72 document.getElementById("setpage").innerHTML = "<div id='setpage'><span id='info'>共 "+totalpage+" 頁 | 第 "+cpage+" 頁</span>" + outstr + "</div>"; 73 outstr = ""; 74 }
3、完整的html源碼(page.html)
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="UTF-8"> 5 <meta name="viewport" content="width=device-width, initial-scale=1"> 6 <title>js靜態分頁</title> 7 <script type="text/javascript" src="http://iyitong.qiniudn.com/jquery-1.11.1.min.js"></script> 8 <!-- 引入分頁js --> 9 <script src="http://7tszs6.com1.z0.glb.clouddn.com/page.js"></script> 10 11 <style> 12 #setpage { margin: 15px auto; text-align: center; } 13 #setpage a:link,#setpage a:visited,#setpage a:hover,#setpage .current,#setpage #info{ 14 border:1px solid #DDD; 15 background:#0d6cbf; 16 display:inline-block; 17 margin:1px; 18 text-decoration:none; 19 text-align:center; 20 color:#fff; 21 padding: 5px 10px; 22 font-size: 16px; 23 border-radius: 5px; 24 } 25 #setpage a:hover { border:1px solid #0d6cbf; background: #0d6cbf; } 26 #setpage .current { 27 border:1px solid #0d6cbf; 28 background: #fff; 29 margin:1px; color: #000; 30 text-decoration: underline; 31 } 32 #setpage #info { width:auto; } 33 </style> 34 35 <script> 36 $(function(){ 37 //靜態頁面時直接調用setpage()方法即可 38 setpage(); 39 // 如果需要通過ajax動態加載json數據,通過以下方式調用 40 // pageIndex,分頁參數,初始值為1 41 // function getData(pageIndex){ 42 // var url = ""; //請求數據的接口 43 // $.getJSON(url,{ 44 // // 這里寫需要傳遞的參數 45 // pageIndex:pageIndex 46 // },function(result){ 47 // // 在這里調用分頁函數 48 // // size:表示返回的數據總數量 49 // var size = result.data.size; 50 // var pagesize = 50;//每頁顯示數據數量 51 // var totalpage=Math.floor((size-1)/pagesize)+1;//總頁數 52 // setpage();//調用分頁 53 // }); 54 // } 55 }); 56 </script> 57 </head> 58 <body> 59 <!-- 這里是顯示分頁的元素 --> 60 <div id="setpage"></div> 61 62 </body> 63 </html>
4、最終的效果圖:


大家有更好的方法,請多多分享交流!
長路漫漫,只因學無止境...
轉發請注明文章出處:http://www.cnblogs.com/iyitong/p/4220824.html 謝謝
