jquery自定義分頁插件(帶回調函數)


插件js:

(function($){
    $.fn.extend({
        pageination:function(options ){
            //面向對象的寫法
         new page(this,options);
        }
        
    })
    var page=function(ele,options){
        if(options==null)
        alert("arg error")
        this.element=ele;
        this.options={
            pageIndex:1,
            pageSize:20,
            pageCount:15,
            callback:function(){}
        };
        this.options=$.extend({},this.options,options);
        this.init();
    }
    //初始化對象
    page.prototype={
        init:function(){
            //初始化
        this.createHtml();
        this.bindEvent();
        },
        createHtml:function(){
            var that=this;
            var content="";
            var pageCount=that.options.pageCount;
            var pageIndex=that.options.pageIndex;
            var pageSize=that.options.pageSize;
            
            content += "<a id=\"firstPage\" class='text'>首頁</a><a id='prePage' class='text'>上一頁</a>";
            //總頁數小於6
            if(pageCount<6){
                for(var i=1; i<pageCount+1;i++){
                    if(pageIndex==i)
                    content+="<a class='current'>"+i+"</a> ";
                    else
                    content+="<a class='other'>"+i+"</a> ";
                }
                
            }else{//總頁數大於6
                if(pageIndex<4){
                    for(var i=1; i<5;i++){
                    if(pageIndex==i)
                    content+="<a class='current'>"+i+"</a> ";
                    else
                    content+="<a class='other'>"+i+"</a> ";
                    }
                    content += ". . .";
                    content += "<a class='other'>"+pageCount+"</a>";
                }
                else if(pageIndex>pageCount-4){
                    content += "<a class='other'>1</a>";
                    content += ". . .";
                    for(var i=pageCount-4; i<pageCount+1;i++){
                      if(pageIndex==i)
                      content+="<a class='current'>"+i+"</a> ";
                      else
                      content+="<a class='other'>"+i+"</a> ";
                    }
                }
                else{
                
                    content += "<a class='other'>1</a>";
                    content += ". . .";
                    for(var i=pageIndex-2; i< parseInt(pageIndex)+3;i++){
                      if(pageIndex==i)
                      content+="<a class='current'>"+i+"</a> ";
                      else
                      content+="<a class='other'>"+i+"</a> ";
                    }
                    content += ". . .";
                    content += "<a class='other'>"+pageCount+"</a>";
                }
                
            }
            //結尾工作
            content += "<a id='nextPage'  class='text'>下一頁</a>";
            content += "<a id=\"lastPage\"  class='text' >尾頁</a>";
            content += "<span class='totalPages'> 共<span>"+pageCount+"</span>頁 </span>";
            content += "<span class='totalSize'> 共<span>"+pageSize+"</span>條記錄 </span>";
            that.element.html(content);
            
        },
        bindEvent:function(){ 
            var that=this;
            that.element .on('click','a',function(){
                var index=$(this).html();
                var current=that.options.pageIndex;
                var id=$(this).attr("id");
                if(id=="nextPage"){
                    current++;
                    if(current>that.options.pageCount){
                        current=that.options.pageCount;
                    }
                }
                else if(id=="lastPage"){
                    current=that.options.pageCount;
                }
                else if(id=="firstPage"){
                    current=1;        
                }
                else if(id=="prePage"){
                    current-=1;
                    if(current<0){
                        current=1;
                    }
                }else{
                    current=index;
                }
                that.options.pageIndex=current;
                that.createHtml();
                
                //回調函數
                if(that.options.callback){
                    that.options.callback($(this));
                }
            })
        }
        
    }
    
})(window.jQuery);

 

 

前台樣式:

 1 <style>
 2             
 3             #page a.current{
 4                  background-color: #0073A9;
 5                 border-color: #0073A9;
 6                 color: #FFFFFF;
 7             }
 8             #page a{
 9                 min-width: 30px;
10                 height: 28px;
11                 border: 1px solid #dce0e0!important;
12                 text-align: center;
13                 margin: 0 4px;
14                 cursor: pointer;
15                 line-height: 28px;
16                 color: #666666;
17                 font-size: 13px;
18                 display: inline-block;
19 
20             }
21             #page span span{
22                 color:#0073A9;
23                 margin: 0,5px;
24             }
25             #page .text{
26                 color:#0073A9;
27             }
28         </style>
主頁面代碼:
<div id="page"></div>
 
         

調用代碼:

<script type="text/javascript"    >
    
     $("#page").pageination({
              pageCount: 10,
              pageSize: 300,
              callback: function(ele) {
                      //alert(ele.html())
                       } 
           } );
        
</script>

效果:

值得注意的是

1.引入js前 需要提前引入 jquery js文件

2.由於樣式重疊 在寫前台樣式的時候 需要對使用到的標簽的選擇器寫的更具體

如:

 
 #page a.current{
                 background-color: #0073A9;
                 border-color: #0073A9;
                 color: #FFFFFF;
             }
 
         

 

 
         
 
         
 
        

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM