javascript实现前端分页效果


Posted in Javascript onJune 24, 2020

本文实例为大家分享了javascript实现前端分页效果的具体代码,供大家参考,具体内容如下

需求:实现分页请求表格数据,ajax暂时没写,只写了分页的功能。

效果图:

当页数是第一页的时候,首页和向前那个按钮处于禁止点击的状态

javascript实现前端分页效果

各个按钮都正常的状态

javascript实现前端分页效果

当页数是第一页的时候,首页和向前那个按钮处于禁止点击的状态

javascript实现前端分页效果

各部分的代码如下:

html部分:

<!-- 分页 -->
 <div class="pageBox">
 <div class="pageTotal">共<span id="dataLength">88</span>条</div>
 <div class="pageContent">
  <button class='firstPage'>首页</button>
  <button class="prevPage"></button>
  <button class="showPage"></button>
  <button class="nextPage"></button>
  <button class="lastPage">尾页</button>
 </div>
 <div class="selectSize">
  <div><span class="numSelect">10</span> <span>条/页</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>页</span></div>
 <ul class="pageSelectShow">
  <li data-num="10">10条/页</li>
  <li data-num="20">20条/页</li>
  <li data-num="50">50条/页</li>
  <li data-num="100">100条/页</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代码:

//点击显示选择条数的div
 var showFlag = true;
 var numcount = 1;//默认第一页
 var dataLength =10000;
 $('#dataLength').text(dataLength);
 var allCount = Math.ceil(dataLength / 10);
 console.log(allCount);
 //分页跳转
 $('.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;
  }
 })
 //点击选择条数
 //

 $('.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' + ')'
  })
 })

 //点击首页
 $('.firstPage').click(function () {
  numcount = 1;
  $('.showPage').text(numcount);
  firstDis(true, 'not-allowed', '0.5')
  lastDis(false, 'pointer', '1')
 })
 //点击上一页
 $('.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')
  }
 })
 //点击下一页
 $('.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')
  }
 })
 //点击尾页
 $('.lastPage').click(function () {
  numcount = allCount
  $('.showPage').text(allCount);
  firstDis(false, 'pointer', '1')
  lastDis(true, 'not-allowed', '0.5')
 })
 //当页码为1,禁止点击的函数
 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
  })
 }
 //当页码为20,禁止点击的函数
 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('输入错误');
   e.target.value = ''
  }
  
  
  }
})

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
js 获取计算后的样式写法及注意事项
Feb 25 Javascript
MyEclipse取消验证Js的两种方法
Nov 14 Javascript
showModalDialog在谷歌浏览器下会返回Null的解决方法
Nov 27 Javascript
基于jquery实现的可编辑下拉框实现代码
Aug 02 Javascript
AngularJS中取消对HTML片段转义的方法例子
Jan 04 Javascript
jquery复选框多选赋值给文本框的方法
Jan 27 Javascript
jQuery DOM插入节点操作指南
Mar 03 Javascript
JavaScript中用于生成随机数的Math.random()方法
Jun 15 Javascript
JavaScript电子时钟倒计时
Jan 09 Javascript
JS常见简单正则表达式验证功能小结【手机,地址,企业税号,金额,身份证等】
Jan 22 Javascript
bootstrap vue.js实现tab效果
Feb 07 Javascript
javascript数据类型详解
Feb 07 Javascript
JS实现多选框的操作
Jun 24 #Javascript
微信小程序实现发微博功能的示例代码
Jun 24 #Javascript
JavaScript实现答题评分功能页面
Jun 24 #Javascript
JS实现电脑虚拟键盘打字测试
Jun 24 #Javascript
JS实现简单打字测试
Jun 24 #Javascript
微信小程序实现多选框功能的实例代码
Jun 24 #Javascript
JS实现电脑虚拟键盘的操作
Jun 24 #Javascript
You might like
一个PHP操作Access类(PHP+ODBC+Access)
2007/01/02 PHP
php对二维数组进行排序的简单实例
2013/12/19 PHP
CodeIgniter配置之SESSION用法实例分析
2016/01/19 PHP
thinkphp分页实现效果
2016/10/13 PHP
thinkPHP自动验证机制详解
2016/12/05 PHP
jQuery contains过滤器实现精确匹配使用方法
2013/04/12 Javascript
使用GruntJS链接与压缩多个JavaScript文件过程详解
2013/08/02 Javascript
jquery代码实现简单的随机图片瀑布流效果
2015/04/20 Javascript
jQuery Real Person验证码插件防止表单自动提交
2015/11/06 Javascript
Angular4 中常用的指令入门总结
2017/06/12 Javascript
vue2.0 axios跨域并渲染的问题解决方法
2018/03/08 Javascript
vue 使用html2canvas将DOM转化为图片的方法
2018/09/11 Javascript
React性能优化系列之减少props改变的实现方法
2019/01/17 Javascript
vue响应式系统之observe、watcher、dep的源码解析
2019/04/09 Javascript
优雅的将ElementUI表格变身成树形表格的方法步骤
2019/04/11 Javascript
JavaScript:ES2019 的新特性(译)
2019/08/08 Javascript
node删除、复制文件或文件夹示例代码
2019/08/13 Javascript
JS自定义对象创建与简单使用方法示例
2020/01/15 Javascript
跟老齐学Python之集合的关系
2014/09/24 Python
Python之pandas读写文件乱码的解决方法
2018/04/20 Python
python中自带的三个装饰器的实现
2019/11/08 Python
Python字典中的值为列表或字典的构造实例
2019/12/16 Python
python3 字符串知识点学习笔记
2020/02/08 Python
基于spring boot 日志(logback)报错的解决方式
2020/02/20 Python
python 使用事件对象asyncio.Event来同步协程的操作
2020/05/04 Python
python+selenium 简易地疫情信息自动打卡签到功能的实现代码
2020/08/22 Python
python如何对链表操作
2020/10/10 Python
写好自荐信的几个要点
2013/12/26 职场文书
办公室前台岗位职责
2014/01/04 职场文书
《埃及的金字塔》教学反思
2014/04/07 职场文书
四风问题个人剖析材料
2014/10/07 职场文书
质量保证书格式模板
2015/02/27 职场文书
鸡毛信观后感
2015/06/11 职场文书
2019数学教师下学期工作总结
2019/06/27 职场文书
mysql优化
2021/04/06 MySQL
vue数据字典取键值项目的字典问题
2022/04/12 Vue.js