JS實(shí)現(xiàn)分頁(yè)導(dǎo)航效果
前言
最近的項(xiàng)目需要添加一個(gè)分頁(yè)導(dǎo)航的功能,沒(méi)有用網(wǎng)上封裝好的文件。通過(guò)JS自己簡(jiǎn)單實(shí)現(xiàn)了效果。下面和大家分享一下。
內(nèi)容
下圖為首次加載后的效果,默認(rèn)顯示5頁(yè),

當(dāng)點(diǎn)擊下一頁(yè)時(shí)將選中頁(yè)面的頁(yè)碼置于中間

下面讓我們來(lái)看看實(shí)現(xiàn)的代碼
第一部分是在頁(yè)面顯示內(nèi)容的處理
function SetListPage() {
$.ajax({
type: "GET",
url: "ajax/PhoneList.ashx?",
datatype: 'json',
success: function (data, textStatus) {
var li_list = "";
if (data != "") {
var cc = jQuery.parseJSON(data); //轉(zhuǎn)換Json對(duì)象
var pagesize = 6; //設(shè)置每頁(yè)顯示數(shù)
var pagecount = Math.ceil(cc.length / pagesize); //獲取頁(yè)數(shù)
SetPageCount(pagecount); //設(shè)置跳轉(zhuǎn)頁(yè)簽
for (var j = 0, l = pagecount; j < l; j++) { //設(shè)置頁(yè)面內(nèi)容
if (j == 0) {
li_list += "<table class='phonetable' >";
}
else {
li_list += "<table class='phonetable hide'>";
}
li_list += "<tr>";
li_list += "<th>姓名</th>";
li_list += "<th>手機(jī)號(hào)碼</th>";
li_list += "<th>郵箱</th>";
li_list += "</tr> ";
var index = j * pagesize;
var rowcount = j * pagesize + pagesize;
if (rowcount > cc.length) {
rowcount = cc.length;
}
for (var i = index; i < rowcount; i++) {
var Name = cc[i]['Name'];
var PhoneNO = cc[i]['PhoneNO'];
var Email = cc[i]['Email'];
li_list += "<tr>";
li_list += "<td>" + Name + "</td>";
li_list += "<td>" + PhoneNO + "</td>";
li_list += "<td>" + Email + "</td>";
li_list += "</tr> ";
}
li_list += "</table>";
}
}
}
});
}
第二部分是動(dòng)態(tài)的設(shè)置頁(yè)碼并添加頁(yè)碼導(dǎo)航的方法
function SetPageCount(count) {
if (count > 0) { //設(shè)置動(dòng)態(tài)頁(yè)碼
var li_list = "";
li_list += "<ul>";
li_list += "<li id='01preage'><a class='prev'><span></span>上一頁(yè)</a></li>";
li_list += "<li><ul>";
li_list += "<li class='01pageIndex'><a class='active'>1</a></li>";
for (var i = 2; i <= count; i++) {
if (i <= 5) {
li_list += "<li class='01pageIndex'><a>" + i + "</a></li>";
} else {
li_list += "<li class='01pageIndex'><a style='display: none;'>" + i + "</a></li>";
}
}
li_list += "</ul></li>";
li_list += "<li id='01nextage'><a>下一頁(yè)<span></span></a></li>";
li_list += "</ul>";
if (li_list != null && li_list.length > 0) {
$("#PhonePageCount").html(li_list);
$('.01pageIndex a').click(function () { //添加添加分頁(yè)導(dǎo)航的事件
var pagecounts = $('.01pageIndex a').length;
$(this).addClass('active');
$(this).parent().siblings().find('a').removeClass('active');
var index = $(this).parent().index() || 0;
if (1 < index && index < pagecounts - 2) {
$('.01pageIndex a').hide()
$('.01pageIndex a').eq(index - 2).show();
$('.01pageIndex a').eq(index - 1).show();
$('.01pageIndex a').eq(index).show();
$('.01pageIndex a').eq(index + 1).show();
$('.01pageIndex a').eq(index + 2).show();
}
$('#phonelist>table').siblings().hide();
$('#phonelist>table').eq(index).show();
})
$('#01preage').click(function () {
var currentPageIndex = $('.01pageIndex').find("a[class$='active']").parent().index();
var pagecounts = $('.01pageIndex a').length;
if (currentPageIndex > 0) {
var thisobj = $('.01pageIndex a').eq(currentPageIndex - 1);
thisobj.addClass('active');
thisobj.parent().siblings().find('a').removeClass('active');
if (0 < currentPageIndex && currentPageIndex < pagecounts - 3) {
$('.01pageIndex a').hide()
$('.01pageIndex a').eq(currentPageIndex - 3).show();
$('.01pageIndex a').eq(currentPageIndex - 2).show();
$('.01pageIndex a').eq(currentPageIndex - 1).show();
$('.01pageIndex a').eq(currentPageIndex).show();
$('.01pageIndex a').eq(currentPageIndex + 1).show();
}
$('#phonelist>table').siblings().hide();
$('#phonelist>table').eq(currentPageIndex - 1).show();
}
})
$('#01nextage').click(function () {
var currentPageIndex = $('.01pageIndex').find("a[class$='active']").parent().index();
var pagecount = $('.01pageIndex a').length - 1;
var pagecounts = $('.01pageIndex a').length;
if (pagecount > currentPageIndex) {
var thisobj = $('.01pageIndex').eq(currentPageIndex + 1);
thisobj.find('a').addClass('active');
thisobj.siblings().find('a').removeClass('active');
if (0 < currentPageIndex && currentPageIndex < pagecounts - 3) {
$('.01pageIndex a').hide()
$('.01pageIndex a').eq(currentPageIndex - 1).show();
$('.01pageIndex a').eq(currentPageIndex).show();
$('.01pageIndex a').eq(currentPageIndex + 1).show();
$('.01pageIndex a').eq(currentPageIndex + 2).show();
$('.01pageIndex a').eq(currentPageIndex + 3).show();
}
$('#phonelist').siblings().hide();
$('#phonelist>table').eq(currentPageIndex + 1).show();
}
})
}
}
}
小結(jié)
一個(gè)小小的功能,在實(shí)現(xiàn)的過(guò)程中并不容易不斷的調(diào)試和優(yōu)化才讓這樣的需求得到了合理的實(shí)現(xiàn)。但敲代碼中也讓我更多的感受到了頁(yè)面導(dǎo)航中所需要考慮到的多元素設(shè)計(jì)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- JS實(shí)現(xiàn)滑動(dòng)導(dǎo)航效果
- JS實(shí)現(xiàn)音樂(lè)導(dǎo)航特效
- JS實(shí)現(xiàn)導(dǎo)航欄樓層特效
- JS中用三種方式實(shí)現(xiàn)導(dǎo)航菜單中的二級(jí)下拉菜單
- JS實(shí)現(xiàn)選中當(dāng)前菜單后高亮顯示的導(dǎo)航條效果
- 一個(gè)js控制的導(dǎo)航菜單實(shí)例代碼
- CSS3+Js實(shí)現(xiàn)響應(yīng)式導(dǎo)航條
- JS 實(shí)現(xiàn)導(dǎo)航欄懸停效果
- js實(shí)現(xiàn)無(wú)限級(jí)樹(shù)形導(dǎo)航列表效果代碼
- js+css實(shí)現(xiàn)扇形導(dǎo)航效果
相關(guān)文章
JS操作字符串轉(zhuǎn)換為數(shù)值并取整的代碼
這篇文章主要介紹了JS操作字符串轉(zhuǎn)換為數(shù)值并取整的代碼,代碼比較短,需要的朋友可以參考下2014-01-01
javascript實(shí)現(xiàn)鼠標(biāo)拖動(dòng)改變層大小的方法
這篇文章主要介紹了javascript實(shí)現(xiàn)鼠標(biāo)拖動(dòng)改變層大小的方法,涉及javascript操作鼠標(biāo)事件及樣式的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-04-04
父節(jié)點(diǎn)獲取子節(jié)點(diǎn)的字符串示例代碼
這篇文章主要介紹了父節(jié)點(diǎn)獲取子節(jié)點(diǎn)的字符串的方法,需要的朋友可以參考下2014-02-02
JS如何去掉小數(shù)末尾多余的0,并且最多保留兩位小數(shù)
這篇文章主要介紹了JS如何去掉小數(shù)末尾多余的0,并且最多保留兩位小數(shù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04
JavaScript實(shí)現(xiàn)省市聯(lián)動(dòng)效果
這篇文章主要介紹了JavaScript實(shí)現(xiàn)省市聯(lián)動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11
js獲取UserControl內(nèi)容為拼html時(shí)提供方便
js獲取UserControl內(nèi)容時(shí)無(wú)法測(cè)試通過(guò),原來(lái)是繼承了Page 然后使用VerifyRenderingInServerForm驗(yàn)證2014-11-11
JavaScript數(shù)組去重的幾種方法效率測(cè)試
JavaScript數(shù)組去重是前端面試酷愛(ài)的問(wèn)題,問(wèn)題簡(jiǎn)單而又能看出程序員對(duì)計(jì)算機(jī)程序執(zhí)行過(guò)程的理解如何。數(shù)組去重的方法有很多,到底哪種是最理想的我不清楚。于是我測(cè)試了下數(shù)組去重的效率。測(cè)試二十萬(wàn)個(gè)數(shù)據(jù),隨著數(shù)據(jù)越多效率很明顯的就體驗(yàn)了出來(lái)。下面來(lái)一起看看吧。2016-10-10

