最實用的jQuery分頁插件
在做商城和訂單管理的時候,常常會用到分頁功能,所以我封裝了一個jQuery的分頁插件,該插件主要實現(xiàn)上下翻頁,輸入數(shù)字跳轉(zhuǎn)等功能。
具體實現(xiàn)如下:
輸入?yún)?shù)需要當(dāng)前頁碼pageNo,總頁碼totalPage,回調(diào)函數(shù)callback。
主要的實現(xiàn)有兩個函數(shù),一個是根據(jù)當(dāng)前頁和總頁數(shù)生成相應(yīng)的html的代碼,一個是事件綁定及回調(diào)函數(shù)的執(zhí)行。
creatHtml函數(shù):
creatHtml:function(){ var me=this; var content=""; //當(dāng)前頁碼 var current=me.options.pageNo; //總頁碼 var total=me.options.totalPage; //當(dāng)前頁碼大于1顯示向上翻頁按鈕 if(current > 1){ content += "<a><</a>"; } //總頁數(shù)大于7,根據(jù)當(dāng)前頁顯示省略號,否則顯示全部頁碼 if(total > 7){ if(current < 4){ for(var i=1;i<7;i++){ if(current==i){ content += "<a class='current'>"+i+"</a>"; }else{ content += "<a>"+i+"</a>"; } } content += "..."; content += "<a>"+total+"</a>"; }else{ if(current < total - 3){ content += "<a name='1' type='button' class='page num'>1</a>"; content += "..."; for(var i=current-2;i<current+3;i++){ if(current==i){ content += "<a class='current'>"+i+"</a>"; }else{ content += "<a>"+i+"</a>"; } } content += "..."; content += "<a>"+total+"</a>"; }else{ content += "<a>1</a>"; content += "..."; for(var i=total-5;i<total+1;i++){ if(current==i){ content += "<a class='current'>"+i+"</a>"; }else{ content += "<a>"+i+"</a>"; } } } } }else{ for(var i=1;i<total+1;i++){ if(current==i){ content += "<a class='current'>"+i+"</a>"; }else{ content += "<a>"+i+"</a>"; } } } //當(dāng)前頁小于總頁數(shù),顯示向下翻頁按鈕 if(current < total){ content += "<a>></a>"; } //輸入跳轉(zhuǎn) content += " 到第 "; content += "<input max='3' maxlength='3' value='"+current+"' type='text' />"; content += " 頁 "; content += "<a>Go</a>"; //更新HTML me.element.html(content); }
bindEvent函數(shù):
bindEvent:function(){ var me=this; //分頁點擊事件 me.element.on('click','a',function(){ var num=$(this).html(); if(num=="<"){//上翻 me.options.pageNo=+me.options.pageNo-1; }else if(num==">"){//下翻 me.options.pageNo=+me.options.pageNo+1; }else if(num=="Go"){//輸入頁碼跳轉(zhuǎn) var ipt=+me.element.find('input').val(); if(ipt&&ipt<=me.options.totalPage&&ipt!=me.options.pageNo){ me.options.pageNo=ipt; } }else{//直接跳轉(zhuǎn) me.options.pageNo=+num; } //更新html me.creatHtml(); //調(diào)用回調(diào)函數(shù),返回當(dāng)前頁碼 if(me.options.callback){ me.options.callback(me.options.pageNo); } }); }
將函數(shù)封裝起來,完整如下:
;(function($,window,document,undefined){ var initDate={ pageNo:1, totalPage:1, callback:function(){} }; function Paging(element,options){ this.element = element; this.options=options=$.extend(initDate,options||{}); this.init(); } Paging.prototype={ constructor:Paging, init:function(){ this.creatHtml(); this.bindEvent(); }, creatHtml:function(){ var me=this; var content=""; var current=me.options.pageNo; var total=me.options.totalPage; if(current > 1){ content += "<a><</a>"; } if(total > 7){ if(current < 4){ for(var i=1;i<7;i++){ if(current==i){ content += "<a class='current'>"+i+"</a>"; }else{ content += "<a>"+i+"</a>"; } } content += "..."; content += "<a>"+total+"</a>"; }else{ if(current < total - 3){ content += "<a name='1' type='button' class='page num'>1</a>"; content += "..."; for(var i=current-2;i<current+3;i++){ if(current==i){ content += "<a class='current'>"+i+"</a>"; }else{ content += "<a>"+i+"</a>"; } } content += "..."; content += "<a>"+total+"</a>"; }else{ content += "<a>1</a>"; content += "..."; for(var i=total-5;i<total+1;i++){ if(current==i){ content += "<a class='current'>"+i+"</a>"; }else{ content += "<a>"+i+"</a>"; } } } } }else{ for(var i=1;i<total+1;i++){ if(current==i){ content += "<a class='current'>"+i+"</a>"; }else{ content += "<a>"+i+"</a>"; } } } if(current < total){ content += "<a>></a>"; } content += " 到第 "; content += "<input max='3' maxlength='3' value='"+current+"' type='text' />"; content += " 頁 "; content += "<a>Go</a>"; me.element.html(content); }, bindEvent:function(){ var me=this; me.element.on('click','a',function(){ var num=$(this).html(); if(num=="<"){ me.options.pageNo=+me.options.pageNo-1; }else if(num==">"){ me.options.pageNo=+me.options.pageNo+1; }else if(num=="Go"){ var ipt=+me.element.find('input').val(); if(ipt&&ipt<=me.options.totalPage&&ipt!=me.options.pageNo){ me.options.pageNo=ipt; } }else{ me.options.pageNo=+num; } me.creatHtml(); if(me.options.callback){ me.options.callback(me.options.pageNo); } }); } }; $.fn.paging=function(options){ options=$.extend(initDate,options||{}); return new Paging($(this),options); } })(jQuery,window,document);
HTML:
<div id="page"></div>
js引用:
$('#page').paging({pageNo:2,totalPage:10,callback:function(cur){ console.log(‘當(dāng)前頁:'+cur);//當(dāng)前頁:2 }});
這里加了一些簡單的樣式,可以根據(jù)具體的ui設(shè)計來設(shè)計樣式。
<style type="text/css"> a{ width: 23px; height: 23px; border: 1px solid #dce0e0; text-align: center; margin: 0 4px; cursor: pointer; display: inline-block; } .current{ background-color: #5ac3e7; } </style>
github地址:https://github.com/Martian1/jQuery.paging.js
更多精彩內(nèi)容請點擊:jquery分頁功能匯總進行學(xué)習(xí)。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- jQuery Pagination Ajax分頁插件(分頁切換時無刷新與延遲)中文翻譯版
- jquery分頁插件jquery.pagination.js使用方法解析
- Jquery 分頁插件之Jquery Pagination
- 分享一個自己動手寫的jQuery分頁插件
- Ajax分頁插件Pagination從前臺jQuery到后端java總結(jié)
- jQuery ajax分頁插件實例代碼
- 基于bootstrap3和jquery的分頁插件
- jQuery插件分享之分頁插件jqPagination
- jquery ajax分頁插件的簡單實現(xiàn)
- jquery+css3打造一款ajax分頁插件(自寫)
- 使用JQuery實現(xiàn)的分頁插件分享
- jQuery實現(xiàn)的分頁插件完整示例
相關(guān)文章
jquery實現(xiàn)鼠標(biāo)經(jīng)過顯示下劃線的漸變下拉菜單效果代碼
這篇文章主要介紹了jquery實現(xiàn)鼠標(biāo)經(jīng)過顯示下劃線的漸變下拉菜單效果代碼,涉及jquery插件SuperSlide.2.1.js實現(xiàn)滑動切換效果的技巧,非常具有實用價值,需要的朋友可以參考下2015-08-08獲得所有表單值的JQuery實現(xiàn)代碼[IE暫不支持]
通過jquery獲取所有表單值,一般都是后臺語言處理的,這里通過jquery獲取,確實不錯的想法2012-05-05jQueryMobile之Helloworld與頁面切換的方法
這篇文章主要介紹了jQueryMobile之Helloworld與頁面切換的方法,實例分析了jQueryMobile的基礎(chǔ)用法,具有一定參考借鑒價值,需要的朋友可以參考下2015-02-02