您好,登錄后才能下訂單哦!
小編這次要給大家分享的是jQuery如何實現分頁插件,文章內容豐富,感興趣的小伙伴可以來了解一下,希望大家閱讀完這篇文章之后能夠有所收獲。
本文實例講述了jQuery實現的分頁插件。分享給大家供大家參考,具體如下:
呈現
html文件
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> <script src="引入一個jquery文件,這里就不提供了"></script> <link rel="stylesheet" href="引入下邊提供的css文件" rel="external nofollow" > </head> <body> <div id="pages" class="devidePage" ></div> </body> <script> var pages=10; //計算出總頁數(一定要是5的倍數) function getData(num){ /*當前頁數*/ var currentPageNum = num; /*取數據*/ $.ajax({ type: "post", url: url, /*請求的servlet的地址*/ data: {"currentPageNum":currentPageNum}, cache: false, async : false, dataType: "json", success: function (data ,textStatus, jqXHR) { if("true"==data.flag){ setData(data.data); }else{ console.log("不合法!錯誤信息如下:"+data.errorMsg); } }, error:function (XMLHttpRequest, textStatus, errorThrown) { console.log("請求失敗!"); } }); } function setData(data){ /*放數據*/ } </script> <script src="引入下邊提供的js文件"></script> </html>
css文件
@charset "UTF-8"; /*分頁所在的div*/ .devidePage{ margin-top:300px; margin-left: 400px; height: 50px; width: 800px; /* background: gray; */ } /*顯示頁數的div*/ .pages{ float:left; margin-left:2px; height:50px; width:50px; background: #EEEEEE; text-align:center; line-height:50px; cursor:pointer; } /*首頁*/ .theFirstPage{ float:left; margin-left:2px; height:50px; width:50px; background: #EEEEEE; text-align:center; line-height:50px; cursor:pointer; } /*末頁*/ .theLastPage{ float:left; margin-left:2px; height:50px; width:50px; background: #EEEEEE; text-align:center; line-height:50px; cursor:pointer; } /*上一頁*/ .prePage{ float:left; margin-left:2px; height:50px; width:50px; background: #EEEEEE; text-align:center; line-height:50px; cursor:pointer; } /*下一頁*/ .nextPage{ float:left; margin-left:2px; height:50px; width:50px; background: #EEEEEE; text-align:center; line-height:50px; cursor:pointer; } /*當前頁數*/ .currentPage{ float:left; margin-left:2px; height:50px; width:100px; background: #EEEEEE; text-align:center; line-height:50px; } /*總頁數*/ .pageNums{ float:left; margin-left:2px; height:50px; width:100px; background: #EEEEEE; text-align:center; line-height:50px; } /*輸入頁數*/ .jump{ float:left; margin-left:2px; height:48px; width:50px; border:0.5px solid #EEEEEE; } /*跳轉*/ .jumpClick{ float:left; margin-left:2px; height:50px; width:50px; background: #EEEEEE; text-align:center; line-height:50px; cursor:pointer; }
js文件
/** * 俠 2018-8-15 */ function loadAll() { var theFirstPage = "<div class=\"theFirstPage\" οnclick=\"theFirstPage()\">首頁</div>"; var prePage = "<div class=\"prePage\" οnclick=\"prePage()\">上一頁</div>"; var pagess = "<div id=\"page_1\" class=\"pages\" οnclick=\"changePage(this.id)\">1</div>" + "<div id=\"page_2\" class=\"pages\" οnclick=\"changePage(this.id)\">2</div>" + "<div id=\"page_3\" class=\"pages\" οnclick=\"changePage(this.id)\">3</div>" + "<div id=\"page_4\" class=\"pages\" οnclick=\"changePage(this.id)\">4</div>" + "<div id=\"page_5\" class=\"pages\" οnclick=\"changePage(this.id)\">5</div>"; var nextPage = "<div class=\"nextPage\" οnclick=\"nextPage()\">下一頁</div>"; var theLastPage = "<div class=\"theLastPage\" οnclick=\"theLastPage()\">末頁</div>"; var currentPages = "<div id=\"currentPage\" class=\"currentPage\">第1頁</div>"; var pageNums = "<div id=\"pageNums\" class=\"pageNums\">共" + pages + "頁</div>"; var jump = "<input id=\"jump\" type=\"text\" class=\"jump\" " +"οnkeyup=\"(this.v=function(){this.value=this.value.replace(/[^0-9-]+/,'');}).call(this)\"" +" οnblur=\"this.v();\">"; var jumpClick = "<div class=\"jumpClick\" οnclick=\"jump()\">跳轉</div>"; $("#pages").html(theFirstPage + prePage + pagess + nextPage + theLastPage + currentPages + pageNums + jump + jumpClick); } loadAll(); function defultBackground() { $("#page_1").css("background", "#66b2ff"); //配置選中顏色 } defultBackground(); function changeBackground() { $(".pages").css("background", "#EEEEEE"); //配置默認顏色 for (var i = 0; i < 5; i++) { if ($("#page_" + (i + 1)).text() == $("#currentPage").text().split("第")[1] .split("頁")[0]) { $("#page_" + (i + 1)).css("background", "#66b2ff"); //配置選中顏色 break; } } } function theFirstPage(){ $('#currentPage').html("第" + 1 + "頁"); $("#page_1").html(1); $("#page_2").html(2); $("#page_3").html(3); $("#page_4").html(4); $("#page_5").html(5); changeBackground(); getData(getCurrentPageNum()); } function theLastPage(){ $('#currentPage').html("第" + pages + "頁"); $("#page_1").html(pages-4); $("#page_2").html(pages-3); $("#page_3").html(pages-2); $("#page_4").html(pages-1); $("#page_5").html(pages); changeBackground(); getData(getCurrentPageNum()); } function changePage(id) { var pagenum = parseInt($("#" + id).text()) - 1; $('#currentPage').html("第" + $("#" + id).text() + "頁"); if ((id.split("_")[1] == 1) && (parseInt($("#" + id).text()) > 1)) { $("#page_1").html(parseInt($("#page_1").text()) - 1); $("#page_2").html(parseInt($("#page_2").text()) - 1); $("#page_3").html(parseInt($("#page_3").text()) - 1); $("#page_4").html(parseInt($("#page_4").text()) - 1); $("#page_5").html(parseInt($("#page_5").text()) - 1); } if ((id.split("_")[1] == 5) && (parseInt($("#" + id).text()) < pages)) { $("#page_1").html(parseInt($("#page_1").text()) + 1); $("#page_2").html(parseInt($("#page_2").text()) + 1); $("#page_3").html(parseInt($("#page_3").text()) + 1); $("#page_4").html(parseInt($("#page_4").text()) + 1); $("#page_5").html(parseInt($("#page_5").text()) + 1); } changeBackground(); getData(getCurrentPageNum()); } function prePage() { var currentPageNumStr = $("#currentPage").text().split("第")[1].split("頁")[0]; var currentPageNum = parseInt(currentPageNumStr); if (currentPageNum > 1) { var toPageNum = currentPageNum - 1; $("#currentPage").html("第" + toPageNum + "頁"); if ((currentPageNum > 1) && ($("#page_1").text() != 1)) { $("#page_1").html(parseInt($("#page_1").text()) - 1); $("#page_2").html(parseInt($("#page_2").text()) - 1); $("#page_3").html(parseInt($("#page_3").text()) - 1); $("#page_4").html(parseInt($("#page_4").text()) - 1); $("#page_5").html(parseInt($("#page_5").text()) - 1); } changeBackground(); getData(getCurrentPageNum()); } else { } } function nextPage() { var currentPageNumStr = $("#currentPage").text().split("第")[1].split("頁")[0]; var currentPageNum = parseInt(currentPageNumStr); if (currentPageNum < pages) { var toPageNum = currentPageNum + 1; $("#currentPage").html("第" + toPageNum + "頁"); if (currentPageNum >= 5 && ($("#page_5").text() != pages)) { $("#page_1").html(parseInt($("#page_1").text()) + 1); $("#page_2").html(parseInt($("#page_2").text()) + 1); $("#page_3").html(parseInt($("#page_3").text()) + 1); $("#page_4").html(parseInt($("#page_4").text()) + 1); $("#page_5").html(parseInt($("#page_5").text()) + 1); } changeBackground(); getData(getCurrentPageNum()); } else { } } function jump() { var numstr = $("#jump").val(); var num = parseInt(numstr); if ((num < 1) || (num > pages)) { alert("輸入不合法"); $("#jump").val(1); } else { $("#currentPage").html("第" + num + "頁"); if (num >= 5) { $("#page_5").html(num); $("#page_4").html(num - 1); $("#page_3").html(num - 2); $("#page_2").html(num - 3); $("#page_1").html(num - 4); } else { if (num = 4) { $("#page_5").html(num + 1); $("#page_4").html(num); $("#page_3").html(num - 1); $("#page_2").html(num - 2); $("#page_1").html(num - 3); } if (num = 3) { $("#page_5").html(num + 2); $("#page_4").html(num + 1); $("#page_3").html(num); $("#page_2").html(num - 1); $("#page_1").html(num - 2); } if (num = 2) { $("#page_5").html(num + 3); $("#page_4").html(num + 2); $("#page_3").html(num + 1); $("#page_2").html(num); $("#page_1").html(num - 1); } if (num = 1) { $("#page_5").html(num + 4); $("#page_4").html(num + 3); $("#page_3").html(num + 2); $("#page_2").html(num + 1); $("#page_1").html(num); } } changeBackground(); getData(getCurrentPageNum()); } } function getCurrentPageNum(){ return parseInt( $("#currentPage").text().split("第")[1].split("頁")[0] ); }
看完這篇關于jQuery如何實現分頁插件的文章,如果覺得文章內容寫得不錯的話,可以把它分享出去給更多人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。