Created
July 3, 2014 08:05
-
-
Save shenqihui/4ca4c4e143e908d4bf05 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @name | |
* @desc 页码组装 | |
* @depend ['jQuery'] | |
**/ | |
;(function() { | |
var pageTemp = 1, hash = location.hash; | |
if( /^#page=\d+$/.test(hash) === true ){ | |
pageTemp = parseInt( hash.substring(6) ); | |
} | |
var page = { | |
// 上一页文案 | |
pagePrev: "上一页", | |
// 下一页文案 | |
pageNext: "下一页", | |
// 当前页码 | |
pageNow: pageTemp, | |
// 数据长度 | |
dataLength: 20, | |
// 每页显示的数据长度 | |
perPageLenght: 4, | |
// 进行多少个按钮 | |
pageLength: 5, | |
renderBox: $('.pagination'), | |
} | |
page.lastPage = parseInt( page.dataLength / page.perPageLenght ); | |
function renderPagination(config) { | |
// 新配置 | |
$.extend(page, config); | |
page.lastPage = parseInt( page.dataLength / page.perPageLenght ); | |
if( page.pageNow > page.lastPage ){ | |
location.hash = "page=1"; | |
} | |
// 没有小于 1 的页码的 | |
if( page.pageNow < 1 ){ | |
page.pageNow = 1; | |
} | |
var html = ''; | |
html +='<ul>'; | |
// 组装上一页 | |
if( page.pageNow !== 1){ | |
html += '<li><a href="#page=prev">'+page.pagePrev+'</a></li>' | |
} | |
location.hash = 'page=' + page.pageNow; | |
// 组装中间的页码 | |
var pageNowTemp = page.pageNow - Math.floor( page.pageLength / 2 ); | |
var pageLengthTemp = page.pageLength; | |
while( pageLengthTemp > 0 ){ | |
if( pageNowTemp <= 0 || pageNowTemp > page.lastPage ){ | |
// 不组装小于0 的页码 | |
} else { | |
if( pageNowTemp === page.pageNow ){ | |
html += '<li class="active">'; | |
} else{ | |
html += '<li>' | |
} | |
html += '<a href="#page='+pageNowTemp+'">'+pageNowTemp+'</a> </li>'; | |
} | |
// 下一页计算 | |
pageNowTemp ++; | |
pageLengthTemp --; | |
} | |
// 组装下一页 | |
if( page.lastPage > page.pageNow ){ | |
html += '<li><a href="#page=next">'+page.pageNext+'</a></li>' | |
} | |
html +='</ul>'; | |
page.renderBox.html(html); | |
} | |
$(window).on('renderPagination', function() { | |
renderPagination(); | |
}) | |
$(window).trigger("renderPagination"); | |
$(window).on('hashchange', function() { | |
var hash = location.hash; | |
var pageTemp = 0 ; | |
if( /^#page=\d+$/.test(hash) === true ){ | |
pageTemp = parseInt( hash.substring(6) ); | |
if( page.pageNow !== pageTemp ){ | |
page.pageNow = pageTemp; | |
$(window).trigger("renderPagination"); | |
} | |
} else if ( hash === "#page=next") { | |
// 下一页的 | |
page.pageNow = page.pageNow + 1; | |
// location.hash = "page=nextTrue"; | |
$(window).trigger("renderPagination"); | |
} else if ( hash === "#page=prev") { | |
// 下一页的 | |
page.pageNow = page.pageNow - 1; | |
// location.hash = "page=prevTrue"; | |
$(window).trigger("renderPagination"); | |
} | |
}) | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment