最新版本代碼請移步:https://github.com/pgkk/kkpager
在線演示:https://pgkk.github.io/kkpager/example/pager_test.html
1、總頁數少於 9 頁時,全部顯示;
2、總頁數大於 9 頁時,前兩頁固定顯示,中間以當前頁為中心,前後各留兩個頁碼,其餘用省略號折疊。
輸入框聚焦效果:

常態效果:

var kkpager = {
pagerid : 'div_pager', //容器 ID
pno : 1, //當前頁碼
total : 1, //總頁碼
totalRecords : 0, //總數據條數
isShowTotalPage : true, //是否顯示總頁數
isShowTotalRecords : true, //是否顯示總記錄數
isGoPage : true, //是否顯示頁碼跳轉輸入框
hrefFormer : '', //鏈接前部
hrefLatter : '', //鏈接尾部
/****鏈接算法****/
getLink : function (n) {
//適用於類似:
//hrefFormer = https://www.xx.com/news/20131212
//hrefLatter = .html
//則第 1 頁為 https://www.xx.com/news/20131212.html
//第 2 頁為 https://www.xx.com/news/20131212_2.html
//第 n 頁為 https://www.xx.com/news/20131212_n.html
if (n == 1) {
return this.hrefFormer + this.hrefLatter;
} else {
return this.hrefFormer + '_' + n + this.hrefLatter;
}
},
//跳轉框獲得焦點
focus_gopage : function () {
var btnGo = $('#btn_go');
$('#btn_go_input').attr('hideFocus', true);
btnGo.show();
btnGo.css('left', '0px');
$('#go_page_wrap').css('border-color', '#6694E3');
btnGo.animate({ left: '+=44' }, 50);
},
//跳轉框失去焦點
blur_gopage : function () {
setTimeout(function () {
var btnGo = $('#btn_go');
btnGo.animate({ left: '-=44' }, 100, function () {
$('#btn_go').css('left', '0px');
$('#btn_go').hide();
$('#go_page_wrap').css('border-color', '#DFDFDF');
});
}, 400);
},
//跳轉框頁面跳轉
gopage : function () {
var str_page = $("#btn_go_input").val();
if (isNaN(str_page)) { $("#btn_go_input").val(this.next); return; }
var n = parseInt(str_page);
if (n < 1 || n > this.total) { $("#btn_go_input").val(this.next); return; }
//這裡可以按需改成 window.open
window.location = this.getLink(n);
},
//初始化
init : function (config) {
this.pno = isNaN(config.pno) ? 1 : parseInt(config.pno);
this.total = isNaN(config.total) ? 1 : parseInt(config.total);
this.totalRecords = isNaN(config.totalRecords) ? 0 : parseInt(config.totalRecords);
if (config.pagerid) { this.pagerid = config.pagerid; }
if (config.isShowTotalPage != undefined) { this.isShowTotalPage = config.isShowTotalPage; }
if (config.isShowTotalRecords != undefined) { this.isShowTotalRecords = config.isShowTotalRecords; }
if (config.isGoPage != undefined) { this.isGoPage = config.isGoPage; }
this.hrefFormer = config.hrefFormer || '';
this.hrefLatter = config.hrefLatter || '';
if (config.getLink && typeof(config.getLink) == 'function') { this.getLink = config.getLink; }
//校驗
if (this.pno < 1) this.pno = 1;
this.total = (this.total <= 1) ? 1 : this.total;
if (this.pno > this.total) this.pno = this.total;
this.prv = (this.pno <= 2) ? 1 : (this.pno - 1);
this.next = (this.pno >= this.total - 1) ? this.total : (this.pno + 1);
this.hasPrv = (this.pno > 1);
this.hasNext = (this.pno < this.total);
this.inited = true;
},
//生成分頁控件 HTML
generPageHtml : function () {
if (!this.inited) { return; }
var str_prv = '', str_next = '';
if (this.hasPrv) {
str_prv = '<a href="' + this.getLink(this.prv) + '" title="上一頁">上一頁</a>';
} else {
str_prv = '<span class="disabled">上一頁</span>';
}
if (this.hasNext) {
str_next = '<a href="' + this.getLink(this.next) + '" title="下一頁">下一頁</a>';
} else {
str_next = '<span class="disabled">下一頁</span>';
}
var str = '';
var dot = '<span>...</span>';
var total_info = '';
if (this.isShowTotalPage || this.isShowTotalRecords) {
total_info = '<span class="normalsize">共';
if (this.isShowTotalPage) {
total_info += this.total + '頁';
if (this.isShowTotalRecords) { total_info += ' / '; }
}
if (this.isShowTotalRecords) { total_info += this.totalRecords + '條數據'; }
total_info += '</span>';
}
var gopage_info = '';
if (this.isGoPage) {
gopage_info = ' 轉到' +
'<span id="go_page_wrap" style="display:inline-block;width:44px;height:18px;' +
'border:1px solid #DFDFDF;margin:0px 1px;padding:0px;position:relative;' +
'left:0px;top:5px;">' +
'<input type="button" id="btn_go" onclick="kkpager.gopage();" ' +
'style="width:44px;height:20px;line-height:20px;padding:0px;' +
'font-family:arial,宋體,sans-serif;text-align:center;border:0px;' +
'background-color:#0063DC;color:#FFF;position:absolute;left:0px;top:-1px;' +
'display:none;" value="確定" />' +
'<input type="text" id="btn_go_input" onfocus="kkpager.focus_gopage()" ' +
'onkeypress="if(event.keyCode<48 || event.keyCode>57)return false;" ' +
'onblur="kkpager.blur_gopage()" ' +
'style="width:42px;height:16px;text-align:center;border:0px;' +
'position:absolute;left:0px;top:0px;outline:none;" value="' + this.next + '" />' +
'</span>頁';
}
//分頁主體
if (this.total <= 8) {
for (var i = 1; i <= this.total; i++) {
if (this.pno == i) {
str += '<span class="curr">' + i + '</span>';
} else {
str += '<a href="' + this.getLink(i) + '" title="第' + i + '頁">' + i + '</a>';
}
}
} else {
if (this.pno <= 5) {
for (var i = 1; i <= 7; i++) {
if (this.pno == i) {
str += '<span class="curr">' + i + '</span>';
} else {
str += '<a href="' + this.getLink(i) + '" title="第' + i + '頁">' + i + '</a>';
}
}
str += dot;
} else {
str += '<a href="' + this.getLink(1) + '" title="第1頁">1</a>';
str += '<a href="' + this.getLink(2) + '" title="第2頁">2</a>';
str += dot;
var begin = this.pno - 2;
var end = this.pno + 2;
if (end > this.total) {
end = this.total;
begin = end - 4;
if (this.pno - begin < 2) { begin = begin - 1; }
} else if (end + 1 == this.total) {
end = this.total;
}
for (var i = begin; i <= end; i++) {
if (this.pno == i) {
str += '<span class="curr">' + i + '</span>';
} else {
str += '<a href="' + this.getLink(i) + '" title="第' + i + '頁">' + i + '</a>';
}
}
if (end != this.total) { str += dot; }
}
}
str = " " + str_prv + str + str_next + total_info + gopage_info;
$("#" + this.pagerid).html(str);
}
};
<div id="div_pager"></div>
<script type="text/javascript">
//生成分頁控件
kkpager.init({
pno : '${p.pageNo}', //當前頁
total : '${p.totalPage}', //總頁數
totalRecords : '${p.totalCount}', //總記錄數
hrefFormer : '${hrefFormer}', //鏈接前部
hrefLatter : '${hrefLatter}' //鏈接尾部
});
kkpager.generPageHtml();
</script>
其中 pno、total、totalRecords 是必傳項,取值來自服務端的 pager 對象;可選參數包括 pagerid、isShowTotalPage、isShowTotalRecords、isGoPage、getLink。
鏈接算法需要按自己的路由規則改寫,默認的 getLink 也可以作為 config 參數直接傳入覆蓋。
#div_pager{
clear:both; height:30px; line-height:30px;
margin-top:20px; color:#999999;
}
#div_pager a{
padding:4px 8px; margin:10px 3px; font-size:12px;
border:1px solid #DFDFDF; background-color:#FFF;
color:#9d9d9d; text-decoration:none;
}
#div_pager span{
padding:4px 8px; margin:10px 3px; font-size:14px;
}
#div_pager span.disabled{
padding:4px 8px; margin:10px 3px; font-size:12px;
border:1px solid #DFDFDF; background-color:#FFF; color:#DFDFDF;
}
#div_pager span.curr{
padding:4px 8px; margin:10px 3px; font-size:12px;
border:1px solid #FF6600; background-color:#FF6600; color:#FFF;
}
#div_pager a:hover{
background-color:#FFEEE5; border:1px solid #FF6600;
}
#div_pager span.normalsize{ font-size:12px; }
1、無數據或只有一頁時:

2、多頁時:

3、第 5 頁:

4、第 6 頁(頁碼窗口右移):

5、第 17 頁(接近尾頁):

6、尾頁:

7、輸入框聚焦:

最後提醒一句:真正接入項目時,記得替換 getLink 的鏈接拼接規則,否則跳轉地址會不對。另外示例中輸入框的 ID 是寫死的,樣式也直接內聯,懶得抽出來了,影響不大,各位取用時自行整理即可。