/**
* 说明: 在页面指定元素中构建分页条
* @param curPage 当前第几页
* @param totalPage 一共有多少页
* @param clickHandler 点击事件,传入参数为当前第几页
* @param barCount 分页条共显示多少个按钮
*/
$.fn.pagination = function(curPage, totalPage, clickHandler, barCount){
var pageBarNum = 5;
if(barCount){
pageBarNum = barCount;
}
var tar = $(this).addClass("pagination");
var start = 1;
var end = totalPage;
if(totalPage > pageBarNum){
var index = Math.floor(pageBarNum/2);
var start = (curPage-index) > 0 ? (curPage-index) : 1;
if(totalPage - start <= pageBarNum){
start = totalPage - pageBarNum + 1;
}
var end = start + pageBarNum;
}
var pageHtml = "";
if(curPage > 1){
pageHtml += "<a p='" + (curPage - 1) + "'>«</a>";
}else{
pageHtml += "<a class='disabled'>«</a>";
}
for (var i = start; i < end; i++) {
if (i > totalPage)
break;
if (i == curPage) {
pageHtml += '<a class="disabled">' + i + '</a>';
} else {
pageHtml += "<a p='" + i + "'>" + i + "</a>";
}
}
if(curPage < totalPage){
pageHtml += "<a p='" + (curPage + 1) + "'>»</a>";
}else{
pageHtml += "<a class='disabled'>»</a>";
}
tar.html(pageHtml);
if(clickHandler){
tar.find("a[p]").bind("click", function(){
var page = $(this).attr("p");
clickHandler(page);
});
}
}
样式:
.pagination{
display: inline-block;
}
.pagination a{
display: inline-block;
float: left;
padding: 0 14px;
line-height: 38px;
text-align: center;
border-width: 1px 1px 1px 0px;
border-style: solid;
border-color: #ccc;
font-size:14px;
}
.pagination a:first-child{
border-radius: 3px 0 0 3px;
border-left: 1px solid #ccc;
}
.pagination a:last-child{
border-radius: 0 3px 3px 0;
}
.pagination a:hover{
background-color: whiteSmoke;
text-decoration: none;
}
.pagination .disabled{
background-color: whiteSmoke;
color: #999;
cursor: default;
}
小旭<folyred@gmail.com> 15:49:47
转载请注明原文地址: https://ju.6miu.com/read-1126204.html