javascript實(shí)現(xiàn)前端分頁(yè)效果
本文實(shí)例為大家分享了javascript實(shí)現(xiàn)前端分頁(yè)效果的具體代碼,供大家參考,具體內(nèi)容如下
需求:實(shí)現(xiàn)分頁(yè)請(qǐng)求表格數(shù)據(jù),ajax暫時(shí)沒(méi)寫,只寫了分頁(yè)的功能。
效果圖:
當(dāng)頁(yè)數(shù)是第一頁(yè)的時(shí)候,首頁(yè)和向前那個(gè)按鈕處于禁止點(diǎn)擊的狀態(tài)

各個(gè)按鈕都正常的狀態(tài)

當(dāng)頁(yè)數(shù)是第一頁(yè)的時(shí)候,首頁(yè)和向前那個(gè)按鈕處于禁止點(diǎn)擊的狀態(tài)

各部分的代碼如下:
html部分:
<!-- 分頁(yè) --> <div class="pageBox"> <div class="pageTotal">共<span id="dataLength">88</span>條</div> <div class="pageContent"> <button class='firstPage'>首頁(yè)</button> <button class="prevPage"></button> <button class="showPage"></button> <button class="nextPage"></button> <button class="lastPage">尾頁(yè)</button> </div> <div class="selectSize"> <div><span class="numSelect">10</span> <span>條/頁(yè)</span></div> <div class="icona"></div> </div> <!-- <div id="test1" style="display: inline-block;margin-left: 210px;"></div> --> <div class="goPage"><span>跳至</span><input type="text" id="goPageInp"><span>頁(yè)</span></div> <ul class="pageSelectShow"> <li data-num="10">10條/頁(yè)</li> <li data-num="20">20條/頁(yè)</li> <li data-num="50">50條/頁(yè)</li> <li data-num="100">100條/頁(yè)</li> </ul> </div>
CSS部分:
* {
padding: 0;
margin: 0;
}
body,
html {
width: 100%;
height: 100%;
}
.pageBox{
width: 60%;
margin-left: 20%;
margin-top: 200px;
position: relative;
height: 50px;
}
.pageBox>div{
float: left;
margin: 0 10px;
}
.pageContent>button{
float: left;
margin: 0px 4px;
border: none;
outline: none;
}
.goPage,.pageTotal{
height: 30px;
vertical-align: middle;
font-size: 14px;
}
.goPage{
right: 50px;
}
.goPage span{
display: inline-block;
color: #999999;
}
.goPage input{
display: inline-block;
width: 50px;
height: 30px;
margin: 0px 5px;
border: none;
border: 1px solid #ccc;
border-radius: 4px;
text-align: center;
}
.pageTotal{
left: 50px;
line-height: 30px;
font-size: 15px;
color: #999;
}
.pageTotal span{
margin: 0 3px;
color: #333;
}
.selectSize{
width: 100px;
height: 30px;
border: 1px solid #ccc;
border-radius: 4px;
font-size: 14px;
text-align: center;
line-height: 30px;
vertical-align: middle;
position: relative;
}
.selectSize>div{
float: left;
margin-left: 5px;
}
.icona{
width: 20px;
height: 20px;
background-image: url('./down.png');
background-size: 100% 100%;
background-position: center center;
margin-top: 5px;
cursor: pointer;
position: absolute;
right: 6px;
}
.pageSelectShow{
width: 100px;
height: 162px;
border: 1px solid #ccc;
overflow-y: auto;
position: absolute;
top: -170px;
left: 400px;
list-style: none;
font-size: 15px;
display: none;
background: #fff;
border-radius: 3px;
}
.pageSelectShow li{
width: 100%;
height: 40px;
line-height: 40px;
text-align: center;
cursor: pointer;
}
.pageContent>div{
cursor: pointer;
height: 30px;
}
.firstPage,.lastPage{
width: 60px;
}
.firstPage,.lastPage,.showPage{
background:rgb(67, 133, 255);
color: #fff;
font-size: 15px;
line-height: 30px;
text-align: center;
border-radius: 4px;
}
.showPage{
width: 40px;
}
.prevPage,.nextPage{
height: 30px;
width: 50px;
border: 1px solid #ccc;
border-radius: 4px;
background-repeat: no-repeat;
background-position: center center;
background-size: 20px 20px;
}
.prevPage{
background-image: url('./prev.png');
}
.nextPage{
background-image: url('./next.png');
}
.nowtouch{
color:#009E94
}
JS代碼:
//點(diǎn)擊顯示選擇條數(shù)的div
var showFlag = true;
var numcount = 1;//默認(rèn)第一頁(yè)
var dataLength =10000;
$('#dataLength').text(dataLength);
var allCount = Math.ceil(dataLength / 10);
console.log(allCount);
//分頁(yè)跳轉(zhuǎn)
$('.showPage').text(numcount)
if (numcount === 1) {
firstDis(true, 'not-allowed', '0.5')
}
if (numcount === allCount) {
lastDis(true, 'not-allowed', '0.5')
}
$('.icona').click(function () {
if (showFlag) {
$('.pageSelectShow').css({
'display': 'block'
});
$('.icona').css({
'background-image': 'url(' + './up.png' + ')'
})
showFlag = !showFlag;
} else {
$('.pageSelectShow').css({
'display': 'none'
})
$('.icona').css({
'background-image': 'url(' + './down.png' + ')'
})
showFlag = !showFlag;
}
})
//點(diǎn)擊選擇條數(shù)
//
$('.pageSelectShow li').click(function (e) {
console.log(e.target.innerHTML)
var countLength = e.target.innerHTML
for(var i = 0; i < countLength.length;i++){
console.log(countLength[i])
}
$('.numSelect').text($(this).data('num'));
allCount = Math.ceil(dataLength / e.target.dataset.num);
if(allCount == 1){
firstDis(true, 'not-allowed', '0.5');
lastDis(true, 'not-allowed', '0.5')
}else{
firstDis(true, 'not-allowed', '0.5')
lastDis(false, 'pointer', '1')
}
$(this).addClass('nowtouch').siblings().removeClass('nowtouch')
$('.pageSelectShow').css({
'display': 'none'
})
$('.icona').css({
'background-image': 'url(' + './down.png' + ')'
})
})
//點(diǎn)擊首頁(yè)
$('.firstPage').click(function () {
numcount = 1;
$('.showPage').text(numcount);
firstDis(true, 'not-allowed', '0.5')
lastDis(false, 'pointer', '1')
})
//點(diǎn)擊上一頁(yè)
$('.prevPage').click(function () {
var prevNum = Number($('.showPage').text());
prevNum--;
$('.showPage').text(prevNum);
if (prevNum == numcount) {
firstDis(true, 'not-allowed', '0.5')
} else {
lastDis(false, 'pointer', '1')
}
})
//點(diǎn)擊下一頁(yè)
$('.nextPage').click(function () {
var prevNum = Number($('.showPage').text());
prevNum++
firstDis(false, 'pointer', '1')
$('.showPage').text(prevNum);
if (prevNum == allCount) {
lastDis(true, 'not-allowed', '0.5')
} else {
lastDis(false, 'pointer', '1')
}
})
//點(diǎn)擊尾頁(yè)
$('.lastPage').click(function () {
numcount = allCount
$('.showPage').text(allCount);
firstDis(false, 'pointer', '1')
lastDis(true, 'not-allowed', '0.5')
})
//當(dāng)頁(yè)碼為1,禁止點(diǎn)擊的函數(shù)
function firstDis(boolVal, cursorVal, opacityVal) {
$('.firstPage').attr('disabled', boolVal);
$('.firstPage').css({
'cursor': cursorVal,
'opacity': opacityVal
})
$('.prevPage').attr('disabled', boolVal);
$('.prevPage').css({
'cursor': cursorVal,
'opacity': opacityVal
})
}
//當(dāng)頁(yè)碼為20,禁止點(diǎn)擊的函數(shù)
function lastDis(boolVal, cursorVal, opacityVal) {
$('.lastPage').attr('disabled', boolVal);
$('.lastPage').css({
'cursor': cursorVal,
'opacity': opacityVal
})
$('.nextPage').attr('disabled', boolVal);
$('.nextPage').css({
'cursor': cursorVal,
'opacity': opacityVal
})
}
//鍵盤事件
$('#goPageInp').on('keydown', function (e) {
if (e.keyCode == 13) {
var vals = e.target.value;
console.log(Number(vals));
$(this).blur();
if(Number(vals) && Number(vals) <=allCount ){
$('.showPage').text(vals);
if (vals == allCount) {
firstDis(false, 'pointer', '1')
lastDis(true, 'not-allowed', '0.5')
}
if (vals == numcount) {
lastDis(false, 'pointer', '1')
firstDis(true, 'not-allowed', '0.5')
}
e.target.value = ''
}else{
alert('輸入錯(cuò)誤');
e.target.value = ''
}
}
})
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JS前端認(rèn)證授權(quán)技巧歸納總結(jié)
這篇文章主要為大家介紹了JS前端認(rèn)證授權(quán)技巧歸納總結(jié),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
js尾調(diào)用優(yōu)化的實(shí)現(xiàn)
這篇文章主要介紹了js尾調(diào)用優(yōu)化的實(shí)現(xiàn),尾調(diào)用(Tail Call)是函數(shù)式編程的一個(gè)重要概念,本文介紹它的含義和用法。感興趣的可以了解一下2019-05-05
JavaScript?深拷貝的循環(huán)引用問(wèn)題詳解
如果說(shuō)道實(shí)現(xiàn)深拷貝最簡(jiǎn)單的方法,我們第一個(gè)想到的就是?JSON.stringify()?方法,因?yàn)镴SON.stringify()后返回的是字符串,所以我們會(huì)再使用JSON.parse()轉(zhuǎn)換為對(duì)象,這篇文章主要介紹了JavaScript?深拷貝的循環(huán)引用問(wèn)題,需要的朋友可以參考下2022-12-12
使用Turn.js實(shí)現(xiàn)翻書效果的完整步驟
最近項(xiàng)目經(jīng)理我個(gè)項(xiàng)目練練手,其項(xiàng)目需求是要實(shí)現(xiàn)翻書效果,下面這篇文章主要給大家介紹了關(guān)于使用Turn.js實(shí)現(xiàn)翻書效果的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12
JavaScript實(shí)現(xiàn)圖片自動(dòng)加載的瀑布流效果
這篇文章主要介紹了JavaScript實(shí)現(xiàn)圖片自動(dòng)加載的瀑布流效果的相關(guān)資料,需要的朋友可以參考下2016-04-04
javascript斷點(diǎn)調(diào)試心得分享
javascript中程序是怎么可以中斷執(zhí)行,然后一步一步走下去。而且debug的時(shí)候,可以看到變量,調(diào)用棧等東西。這個(gè)是如何實(shí)現(xiàn)的?2016-04-04

