DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> jQuery入門知識 >> JQuery特效代碼 >> jquery動態分頁效果堪比時光網
jquery動態分頁效果堪比時光網
編輯:JQuery特效代碼     

最近一直研究jquery的分頁效果,剛剛弄好了一個,拿出來與大家分享。分頁效果與時光網的差不多。

先在aspx頁面放置一個<div class="pageDivs"></div> ,這個是用來存放分頁的。

然後建一個page.js文件,具體代碼如下(js中用到的css類是自己設置的,這裡就不給出了,具體的大家可以自己設置一下css樣式):
代碼如下:
$(document).ready(function(){
var pageCount=0;//總頁數,在數據處理的函數裡設定

//////////////////////右部按鈕分頁顯示
function right(pageCount,limit,rlimit){
var html="";
if(parseInt(pageCount)-limit>=rlimit){
for(var i=parseInt(pageCount)-rlimit+1; i<=parseInt(pageCount); i++){
html+="<a page="+i+" leaf='leaf'>"+i+"</a>";}
}
else{
for(var i=parseInt(limit)+1; i<=pageCount; i++){
html+="<a page="+i+" leaf='leaf'>"+i+"</a>";}
}
return html;
}
//////////////////////////首頁,尾頁,上一頁,下一頁
function changeState(pageIndex,pageCount){
var $button1=$("div.pageDivs").find("#Button1");//上一頁
var $button2=$("div.pageDivs").find("#Button2");//下一頁
var $first=$("div.pageDivs").find("#First");//首頁
var $last=$("div.pageDivs").find("#Last");//尾頁
if(parseInt(pageIndex)==1){
$first.css("display","none");
$button1.css("display","none");}
else{
$first.css("display","inline");
$button1.css("display","inline");
$first.attr("page",1);
$button1.attr("page",parseInt(pageIndex)-1);}
if(parseInt(pageIndex)==pageCount){
$button2.css("display","none");
$last.css("display","none");}
else{
$last.css("display","inline");
$button2.css("display","inline");
$last.attr("page",pageCount);
$button2.attr("page",parseInt(pageIndex)+1);}

}
////////////////////////////////span動態分頁 左邊顯示的頁碼個數,右邊顯示的頁碼個數,要求limit>rlimit
function span(pageCount,pageIndex,limit,rlimit){
var isContinue=true;//指示是否繼續執行函數
var html="<a id='First' href='#' >|<</a><a id='Button1' href='#' ><</a>";
var change=(parseInt(pageCount)-parseInt(rlimit))/(parseInt(limit)-2);//指示分頁碼可以變動的次數
if(pageCount!=0&&pageCount!=1){
if(pageCount<=limit){
for(var i=1; i<=pageCount; i++){
html+="<a page="+i+" leaf='leaf'>"+i+"</a>"}
}
else{
if(parseInt(pageIndex)<(limit-2)){
for(var i=1; i<=limit; i++){
html+="<a page="+i+" leaf='leaf'>"+i+"</a>";}
html+="...";
html+=right(pageCount,limit,rlimit);
}
else{
if(parseInt(pageIndex)%(limit-2)==0){
if(parseInt(pageIndex)/(limit-2)<=change&&parseInt(pageIndex)-1+parseInt(limit)-1<=parseInt(pageCount)-parseInt(rlimit)){
for(var i=parseInt(pageIndex)-1; i<parseInt(pageIndex)-1+limit; i++){
html+="<a page="+i+" leaf='leaf'>"+i+"</a>";}
html+="...";
html+=right(pageCount,limit,rlimit);
}
else{
for(var i=1; i<=rlimit; i++){
html+="<a page="+i+" leaf='leaf'>"+i+"</a>";}
html+="...";
var rest=parseInt(pageCount)-parseInt(rlimit);
if(rest<limit){
for(var i=parseInt(rlimit)+1; i<=parseInt(pageCount); i++){
html+="<a page="+i+" leaf='leaf'>"+i+"</a>";}
}
else{
var start=parseInt(pageCount)-parseInt(limit)+1;
for(var i=start; i<=pageCount; i++){
html+="<a page="+i+" leaf='leaf'>"+i+"</a>";}
}
}


}
else{
html=$("div.pageDivs").html();
$("div.pageDivs").html(html);
isContinue=false;
}
}

}
}
if(isContinue){
html+="<a id='Button2' href='#' >></a><a id='Last' href='#' >>|</a>";
$("div.pageDivs").html(html);}
changeState(pageIndex,pageCount);
$("div.pageDivs").find("a[page=" + parseInt(pageIndex) + "]:visible").removeAttr("href").removeClass("disabled").addClass("current").siblings("a[page:visible").removeClass("current").addClass("disabled").attr("href", "#");
}

function page(pageIndex){

/////////////這裡放你具體的數據顯示,使用ajax動態加載處理數據

pageCount="通過數據處理獲得的頁面總數";

span(pageCount,pageIndex,7,2);//對分頁效果進行調用,這裡設置左邊顯示7個頁碼,右邊顯示2個頁碼。

}

//////////////////////////////為頁碼綁定事件

$("div.pageDivs").find("a:visible").live("click",function(){
var result=$(this).attr("page");
if((typeof $(this).attr("leaf"))!= 'undefined'){
$(this).removeAttr("href").removeClass("disabled").addClass("current").siblings().removeClass("current").addClass("disabled").attr("href","#");}

page(result);
});
});

這樣就行了,以上分頁的算法是可以封裝的,與具體的項目沒關系,可以通用。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved