jQuery實現(xiàn)的分頁功能示例
本文實例講述了jQuery實現(xiàn)的分頁功能。分享給大家供大家參考,具體如下:
1、分頁欄HTML碼
<div class="g-cf g-pagerwp"> <div style="visibility:hidden" class="g-pager"> </div> </div>
2、CSS樣式文件
.g-cf:after {clear: both;content: "";display: table;}
.g-cf {zoom:1;}
/*分頁*/
.g-pager{ text-align:center; color: #111111; font: 12px/1.5em Arial,Tahoma; float: right;}
.g-pager a,.g-pager input{ cursor:pointer; border:solid 1px #0F71BE; padding:1px 4px; color:#0F71BE; margin:0 2px; vertical-align:middle; }
.g-pager a.cur,.g-pager a:hover{ background-color:#0F71BE; color:#fff}
.g-pager a.no{ border-color:#A3A3A3; color:#A3A3A3; background-color:#E4F2F9}
.g-pager span{ margin-right:10px; }
.g-pager input{ cursor:default; width:28px; padding:1px 2px; }
.g-pagerwp{ height:23px; line-height:23px;padding:5px; margin-bottom:10px; border: 1px solid #DDDDDD;}
.g-pagerwp .g-btn{ vertical-align:top}
3、JS腳本文件
① 引用JQuery和分頁腳本
<script src="/js/common/jquery-1.6.2.js" type="text/javascript"></script> <script src="/js/jquery.PageBar.js" type="text/javascript"></script>
② 編寫jquery.PageBar.js腳本
/**************************/
//JQuery分頁欄
/**************************/
$.fn.pageBar = function(options) {
var configs = {
PageIndex: 1,
PageSize: 15,
TotalPage: 0,
RecordCount: 0,
showPageCount: 4,
onPageClick: function(pageIndex) {
return false; //默認(rèn)的翻頁事件
}
}
$.extend(configs, options);
var tmp = "",
i = 0,
j = 0,
a = 0,
b = 0,
totalpage = parseInt(configs.RecordCount / configs.PageSize);
totalpage = configs.RecordCount % configs.PageSize > 0 ? totalpage + 1 : totalpage;
tmp += "<span>總記錄數(shù):" + configs.RecordCount + "</span > ";
tmp += " <span>頁數(shù):" + totalpage + "</span>";
if (configs.PageIndex > 1) {
tmp += "<a><</a>"
} else {
tmp += "<a class=\"no\"><</a>"
}
tmp += "<a>1</a>";
if (totalpage > configs.showPageCount + 1) {
if (configs.PageIndex <= configs.showPageCount) {
i = 2;
j = i + configs.showPageCount;
a = 1;
} else if (configs.PageIndex > totalpage - configs.showPageCount) {
i = totalpage - configs.showPageCount;
j = totalpage;
b = 1;
} else {
var k = parseInt((configs.showPageCount - 1) / 2);
i = configs.PageIndex - k;
j = configs.PageIndex + k + 1;
a = 1;
b = 1;
if ((configs.showPageCount - 1) % 2) {
i -= 1
}
}
}
else {
i = 2;
j = totalpage;
}
if (b) {
tmp += "..."
}
for (; i < j; i++) {
tmp += "<a>" + i + "</a>"
}
if (a) {
tmp += " ... "
}
if (totalpage > 1) {
tmp += "<a>" + totalpage + "</a>"
}
if (configs.PageIndex < totalpage) {
tmp += "<a>></a>"
} else {
tmp += "<a class=\"no\">></a>"
}
tmp += "<input type=\"text\" /><a>Go</a>"
var pager = this.html(tmp)
var index = pager.children('input')[0]
pager.children('a').click(function() {
var cls = $(this).attr('class');
if (this.innerHTML == '<') {
if (cls != 'no') {
configs.onPageClick(configs.PageIndex - 2)
}
} else if (this.innerHTML == '>') {
if (cls != 'no') {
configs.onPageClick(configs.PageIndex)
}
} else if (this.innerHTML == 'Go') {
if (!isNaN(index.value)) {
var indexvalue = parseInt(index.value);
indexvalue = indexvalue < 1 ? 1 : indexvalue
indexvalue = indexvalue > totalpage ? totalpage : indexvalue
configs.onPageClick(indexvalue - 1)
}
} else {
if (cls != 'cur') {
configs.onPageClick(parseInt(this.innerHTML) - 1)
}
}
}).each(function() {
if (configs.PageIndex == parseInt(this.innerHTML)) {
$(this).addClass('cur')
}
})
}
③ 初始化使用
$(document).ready(function() {
//設(shè)置分頁信息
var pageOptions = {
AllowPaging: true,
PageIndex: 1, //設(shè)置當(dāng)前頁碼
PageSize: 15, //設(shè)置分頁大小
RecordCount: 1092, //設(shè)置數(shù)據(jù)總數(shù)
TotalPage: 73, //設(shè)置總頁數(shù)
showPageCount: 4,
onPageClick: function(pageIndex) {
alert("您點擊了第" + parseInt(pageIndex + 1) + "頁"); //自定義您的翻頁事件
return false;
}
}
//初始化分頁欄
$('.g-pagerwp .g-pager').css('visibility', 'visible').pageBar(pageOptions);
})
更多關(guān)于jQuery相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《jQuery擴(kuò)展技巧總結(jié)》、《jQuery表格(table)操作技巧匯總》、《jQuery常見經(jīng)典特效匯總》、《jquery選擇器用法總結(jié)》及《jQuery常用插件及用法總結(jié)》
希望本文所述對大家jQuery程序設(shè)計有所幫助。
- jquery pagination分頁插件使用詳解(后臺struts2)
- jquery pagination插件動態(tài)分頁實例(Bootstrap分頁)
- YII框架中搜索分頁jQuery寫法詳解
- PHP+jquery+ajax實現(xiàn)分頁
- jQuery學(xué)習(xí)筆記——jqGrid的使用記錄(實現(xiàn)分頁、搜索功能)
- Laravel+jQuery實現(xiàn)AJAX分頁效果
- jquery datatable服務(wù)端分頁
- 深入理解jQuery layui分頁控件的使用
- Ajax分頁插件Pagination從前臺jQuery到后端java總結(jié)
- jQuery實現(xiàn)的簡單分頁示例
- jQuery DataTables插件自定義Ajax分頁實例解析
- 用jQuery中的ajax分頁實現(xiàn)代碼
- JQuery+Ajax無刷新分頁的實例代碼
相關(guān)文章
JQuery 簡便實現(xiàn)頁面元素數(shù)據(jù)驗證功能
JQuery 簡便實現(xiàn)頁面元素數(shù)據(jù)驗證功能...2007-03-03
javascript權(quán)威指南 學(xué)習(xí)筆記之null和undefined
JavaScript中的關(guān)鍵字null是一個特殊的值,它表示“無值”。null常常被看作對象類型的一個特殊值,即代表“無對象”的值。2011-09-09
jQuery+Ajax+PHP+Mysql實現(xiàn)分頁顯示數(shù)據(jù)實例講解
這是一個典型的Ajax應(yīng)用,在頁面上,您只需要點擊“下一頁”,數(shù)據(jù)區(qū)將自動加載對應(yīng)頁碼的數(shù)據(jù),重新刷新數(shù)據(jù)區(qū)。類似的效果在很多網(wǎng)站上應(yīng)用,尤其在一些需要展示大量圖片數(shù)據(jù)的網(wǎng)頁如淘寶商品列表頁,Ajax分頁效果讓您的網(wǎng)站數(shù)據(jù)加載顯得非常流暢。2015-09-09
仿中關(guān)村在線首頁彈出式廣告插件(jQuery版)
仿中關(guān)村在線首頁彈出式廣告插件(jQuery版) ,需要的朋友可以參考下2012-05-05

