pagination(一个封装好的分页导航)

 

 最近想找一个好看点的分页显示,在国外的一个网站上看到了这样一个分页样式,挺喜欢的,就把它封装成一个可以复用的类

 

代码如下:

text.html

  1. < html >
  2.        < title > 测试 </ title >
  3.      < meta   http-equiv = "pragma"   content = "no-cache" >
  4.      < meta   http-equiv = "cache-control"   content = "no-cache" >
  5.      < meta   http-equiv = "expires"   content = "0" >
  6.      < meta   http-equiv = "Content-Type"   content = "text/html; charset=gb2312" >
  7. < head >
  8.      < script   type = "text/javascript"   src = "pager.js" > </ script >
  9.      < link   rel = "stylesheet"   type = "text/css"   href = "pager.css" >
  10. </ head >
  11.      < body >
  12.          < div   class = "tnt_pagination"   id = "pager" > </ div >
  13.                         
  14.          < script   type = "text/javascript" >
  15.              function confirmsearch(page){
  16.               alert(page);
  17.             }
  18.             
  19.            var  pager  =  new  Pager("pager",5,18,"confirmsearch");
  20.            pager.pageNavigation();
  21.          </ script >
  22.      </ body >

pager.js

  1. function  Pager(id,current,pagenum,fun){
  2.     this .id=id;
  3.     this .current=current;
  4.     this .pagenum=pagenum;
  5.     this .fun=fun;
  6.    
  7.     this .pageNavigation= function (){
  8.        var  low =  this .current - ( this .current%10)+1;
  9.        var  hight = low+9;
  10.        var  content= "<font color='#0072BC'>共" + this .pagenum+ "页 </font>" ;
  11.          if ( this .current >0 &&  this .current%10==0){
  12.           hight =  this .current;
  13.           low = hight-9;
  14.         }
  15.             
  16.          if (low>1){
  17.               var  _previous= "<a href='#' οnclick='"  +  this .fun +  "("  + (low-1) + ")'>Prev</a>"
  18.         } else {
  19.               var  _previous= "<span class='disabled_tnt_pagination'>Prev</span>" ;
  20.         }
  21.         content += _previous;
  22.         
  23.          for ( var  i=low;i<=hight;i++){
  24.              var  urltext;
  25.              if (i== this .current){
  26.               urltext= "<span class='active_tnt_link'>"  +i+  "</span>" ;
  27.             } else {
  28.                 urltext= "<a href='#' οnclick='"  +  this .fun +  "("  + i + ")'>" +i+ "</a>" ;
  29.             }
  30.              if ( this .pagenum<i){
  31.                 urltext= "<span class='disabled_tnt_pagination'>"  +i+  "</span>" ;
  32.             }
  33.             content += urltext;
  34.         }
  35.         
  36.           if (hight<pagenum){
  37.                var  _next= "<a href='#' οnclick='"  +  this .fun +  "("  + (hight+1) + ")'>Next</a>" ;
  38.          } else {
  39.              var  _next= "<span class='disabled_tnt_pagination'>Next</span>" ;
  40.          }
  41.          
  42.          content +=_next;  
  43.          document.getElementById( this .id).innerHTML = content;
  44.       }
  45. }

pager.css

  1. .tnt_pagination {
  2.      display : block ;
  3.      /*text-align:left;*/
  4.      /*height:22px;*/
  5.      /*line-height:21px;*/
  6.      /*clear:both;*/
  7.    padding : 7px ;
  8.      padding-top : 7px ;
  9.      padding-bottom : 2px ;
  10.      text-decoration : none ;
  11.      /*font-family:Arial, Helvetica, sans-serif;*/
  12.      font-size : 12px ;
  13.      font-weight : normal ;
  14. }
  15. .tnt_pagination a:link, .tnt_pagination a:visited{
  16.      padding : 7px ;
  17.      padding-top : 2px ;
  18.      padding-bottom : 2px ;
  19.      border : 1px   solid   #D8D5D5 ;
  20.      margin-left : 7px ;
  21.      text-decoration : none ;
  22.      background-color : #EDF2DD ;
  23.      color : #0072bc ;
  24.      /*width:22px;*/
  25.      font-weight : normal ;
  26. }
  27. .tnt_pagination a:hover {
  28.      background-color : #DDEEFF ;
  29.      border : 1px   solid   #BBDDFF ;
  30.      text-decoration : none ;
  31.      color : #0072BC ;
  32.      font-weight : normal
  33. }
  34. .tnt_pagination .active_tnt_link {
  35.      padding : 7px ;
  36.      padding-top : 2px ;
  37.      padding-bottom : 2px ;
  38.      border : 1px   solid   #BBDDFF ;
  39.      margin-left : 7px ;
  40.      text-decoration : none ;
  41.      background-color : #DDEEFF ;
  42.      color : #0072BC ;
  43.      cursor : default ;
  44. }
  45. .tnt_pagination .disabled_tnt_pagination {
  46.      padding : 7px ;
  47.      padding-top : 2px ;
  48.      padding-bottom : 2px ;
  49.      border : 1px   solid   #C8CCCF ;
  50.      margin-left : 7px ;
  51.      text-decoration : none ;
  52.      background-color : #F5F5F5 ;
  53.      color : #D7D7D7 ;
  54.      cursor : default ;
  55. }

实际运用中只要定义一个div:

<div class="tnt_pagination" id="pager"></div>

 

创建一个类对象,将div的id,当前页数和总页数传进去, :

var pager = new Pager("pager",5,18,"confirmsearch");
     pager.pageNavigation();

 

再定义一个function,用于点击数字的时候执行的操作,一般是跳转到该页数:

 

function confirmsearch(page){
        alert(page);
 }

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值