var
pagination =
function
(o){
var
config = {
count: o.count || 10,
limit: o.limit || 5,
};
var
count = config.count,
limit = config.limit,
eUl = dorea(
".pagination ul"
)[0],
ePre = dorea(
".pagination .page-pre"
)[0],
eNext = dorea(
".pagination .page-next"
)[0],
eUlChild = eUl.children,
pnCount = limit < count ? pnCount = limit : pnCount = count,
midN = Math.ceil( pnCount / 2 );
ePre.setAttribute(
"data-page"
,
"1"
);
eNext.setAttribute(
"data-page"
,
"1"
);
var
renderPag =
function
(startN,currP){
var
childLen = eUlChild.length;
for
(
var
d = childLen-1; d>=0; d-- ) {
eUlChild[d].parentNode.removeChild(eUlChild[d]);
}
for
(
var
i = startN; i <pnCount+startN; i++ ) {
var
eLi = creatEle(
"li"
),
eA = creatEle(
"a"
);
eA.innerHTML = i;
eA.setAttribute(
"href"
,
"javascript:;"
);
eLi.setAttribute(
"data-page"
,i);
eLi.appendChild(eA);
eUl.appendChild(eLi);
eLi.addEventListener(
"click"
,
function
(){
var
currPage =
this
.getAttribute(
"data-page"
);
turnPag(currPage);
ePre.setAttribute(
"data-page"
,currPage);
eNext.setAttribute(
"data-page"
,currPage);
});
}
if
(currP!==undefined) {
if
(currP==
"1"
) {
ePre.style.color =
"#d2d2d2"
;
ePre.style.cursor =
"not-allowed"
;
ePre.removeEventListener(
"click"
,preFn,
false
);
}
else
if
(currP==count){
eNext.style.color =
"#d2d2d2"
;
eNext.style.cursor =
"not-allowed"
;
eNext.removeEventListener(
"click"
,nextFn,
false
);
}
else
{
ePre.style.color =
"#333"
;
ePre.style.cursor =
"pointer"
;
eNext.style.color =
"#333"
;
eNext.style.cursor =
"pointer"
;
ePre.addEventListener(
"click"
,preFn,
false
);
eNext.addEventListener(
"click"
,nextFn,
false
);
}
}
};
var
turnPag =
function
(cp){
console.log(
"当前第 "
+cp+
" 页"
);
if
(count>limit) {
if
( cp<=midN ) {
renderPag(1,cp);
}
else
if
( cp<=count && cp>count - midN ){
renderPag( (count - limit)+1 ,cp) ;
}
else
{
renderPag( (cp-midN)+1 ,cp);
}
}
else
if
(count===limit || count<limit) {
renderPag(1);
}
else
{
renderPag( (count - midN)-1 ,cp);
}
for
(
var
i = 0; i<eUlChild.length; i++) {
eUlChild[i].style.backgroundColor =
"#fff"
;
if
(eUlChild[i].getAttribute(
"data-page"
) == cp) {
eUlChild[i].style.backgroundColor =
"#1E9FFF"
;
}
}
};
var
preFn =
function
(){
var
currPage =
this
.getAttribute(
"data-page"
);
currPage--;
turnPag(currPage);
ePre.setAttribute(
"data-page"
,currPage);
eNext.setAttribute(
"data-page"
,currPage);
};
var
nextFn =
function
(){
var
currPage =
this
.getAttribute(
"data-page"
);
currPage++;
turnPag(currPage);
ePre.setAttribute(
"data-page"
,currPage);
eNext.setAttribute(
"data-page"
,currPage);
};
renderPag(1);
if
(count>limit) {
ePre.style.color =
"#d2d2d2"
;
ePre.style.cursor =
"not-allowed"
;
ePre.removeEventListener(
"click"
,preFn,
false
);
eNext.addEventListener(
"click"
,nextFn,
false
);
}
else
{
ePre.style.color =
"#d2d2d2"
;
ePre.style.cursor =
"not-allowed"
;
ePre.removeEventListener(
"click"
,preFn,
false
);
eNext.style.color =
"#d2d2d2"
;
eNext.style.cursor =
"not-allowed"
;
eNext.removeEventListener(
"click"
,nextFn,
false
);
}
}