JS實(shí)現(xiàn)分頁導(dǎo)航效果
前言
最近的項(xiàng)目需要添加一個(gè)分頁導(dǎo)航的功能,沒有用網(wǎng)上封裝好的文件。通過JS自己簡(jiǎn)單實(shí)現(xiàn)了效果。下面和大家分享一下。
內(nèi)容
下圖為首次加載后的效果,默認(rèn)顯示5頁,
當(dāng)點(diǎn)擊下一頁時(shí)將選中頁面的頁碼置于中間
下面讓我們來看看實(shí)現(xiàn)的代碼
第一部分是在頁面顯示內(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è)置每頁顯示數(shù) var pagecount = Math.ceil(cc.length / pagesize); //獲取頁數(shù) SetPageCount(pagecount); //設(shè)置跳轉(zhuǎn)頁簽 for (var j = 0, l = pagecount; j < l; j++) { //設(shè)置頁面內(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è)置頁碼并添加頁碼導(dǎo)航的方法
function SetPageCount(count) { if (count > 0) { //設(shè)置動(dòng)態(tài)頁碼 var li_list = ""; li_list += "<ul>"; li_list += "<li id='01preage'><a class='prev'><span></span>上一頁</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>下一頁<span></span></a></li>"; li_list += "</ul>"; if (li_list != null && li_list.length > 0) { $("#PhonePageCount").html(li_list); $('.01pageIndex a').click(function () { //添加添加分頁導(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)的過程中并不容易不斷的調(diào)試和優(yōu)化才讓這樣的需求得到了合理的實(shí)現(xiàn)。但敲代碼中也讓我更多的感受到了頁面導(dǎo)航中所需要考慮到的多元素設(shè)計(jì)。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- JS實(shí)現(xiàn)滑動(dòng)導(dǎo)航效果
- JS實(shí)現(xiàn)音樂導(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)無限級(jí)樹形導(dǎo)航列表效果代碼
- js+css實(shí)現(xiàn)扇形導(dǎo)航效果
相關(guān)文章
JS排序算法之希爾排序與快速排序?qū)崿F(xiàn)方法
這篇文章主要介紹了JS排序算法之希爾排序與快速排序?qū)崿F(xiàn)方法,結(jié)合實(shí)例形式分析了希爾排序與快速排序的原理及javascript實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-12-12javascript實(shí)現(xiàn)仿IE頂部的可關(guān)閉警告條
仿windows IE頂部的敬告工具條,帶關(guān)閉按鈕,設(shè)計(jì)還算精美,你完全可以用到自己的網(wǎng)頁用于顯示提示等方面,有需要的小伙伴可以參考下。2015-05-05解析John Resig Simple JavaScript Inheritance代碼
上網(wǎng)也查了一下對(duì)些的理解說的都不是很清楚. 在翻閱的同時(shí)找到了一篇 分析這篇文章的文章 哈哈 分析的很詳細(xì). (Join Resig 大師的 "Simple Inheritance" 使用了很多有意思的技巧) 如果你有時(shí)間, 并對(duì)此感興趣不訪好好看看. 我相信多少會(huì)有所收益的.2012-12-12JS碰撞運(yùn)動(dòng)實(shí)現(xiàn)方法詳解
這篇文章主要介紹了JS碰撞運(yùn)動(dòng)實(shí)現(xiàn)方法,詳細(xì)分析了碰撞運(yùn)動(dòng)的原理及相應(yīng)的javascript實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-12-12JavaScript offset實(shí)現(xiàn)鼠標(biāo)坐標(biāo)獲取和窗口內(nèi)模塊拖動(dòng)
在頁面開發(fā)時(shí)我們少不了各種鼠標(biāo)交互動(dòng)作,那么JavaScript中如何實(shí)現(xiàn)鼠標(biāo)坐標(biāo)獲取和窗口內(nèi)模塊拖動(dòng),本文就詳細(xì)的介紹一下,感興趣的可以了解一下2021-05-05用javascript讀取xml文件讀取節(jié)點(diǎn)數(shù)據(jù)
這篇文章主要介紹了用javascript讀取xml文件讀取節(jié)點(diǎn)數(shù)據(jù)的具體實(shí)現(xiàn),需要的朋友可以參考下2014-08-08WebStorm ES6 語法支持設(shè)置&babel使用及自動(dòng)編譯(詳解)
下面小編就為大家?guī)硪黄猈ebStorm ES6 語法支持設(shè)置&babel使用及自動(dòng)編譯(詳解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09