jquery.pager.js实现分页效果


Posted in jQuery onJuly 29, 2019

本文实例为大家分享了jquery.pager.js实现分页的具体代码,供大家参考,具体内容如下

1.html

<link href="/stylesheets/Pager.css" rel="stylesheet" type="text/css"/>
 <script src="/javascripts/web/jquery.pager.js" type="text/javascript"></script>


<body>
   <div id="pager">
    <div id="countPage" style="display:none">{{maxPage}}</div>
    <div id="title" style="display:none">{{title}}</div>
    <div id="nowPage" style="display:none">{{page}}</div>
   </div>
</body>
<script type="text/javascript">
 var title =jQuery("#title").text() ;
 var pageCount =parseInt(jQuery("#countPage").text()) ;
 var nowPage=parseInt(jQuery("#nowPage").text());
 jQuery(function(){
  $("#pager").pager({ pagenumber: nowPage, pagecount: pageCount, buttonClickCallback: PageClick });//pagenumber:当前页,pagecount:总页数
 });
 PageClick = function(pageclickednumber) {
  $("#pager").pager({
   pagenumber: pageclickednumber, pagecount: pageCount, buttonClickCallback: PageClick
  });
  window.location.href = "/article/search/"+title+"/"+pageclickednumber;
 }
</script>

2.Pager.css

#pager ul.pages{
 display:block;
 border:none;
 text-transform:uppercase;
 font-size:10px;margin:10px 0 50px;padding:0;
}
#pager ul.pages li{
 list-style:none;
 float:left;
 border:1px solid #ccc;
 text-decoration:none;
 margin:-20px 5px 0 0;padding:5px;
}
#pager ul.pages li:hover{border:1px solid #003f7e; }
#pager ul.pages li.pgEmpty{
 border:1px solid #eee;
 color:#eee;
}
#pager ul.pages li.pgCurrent{
 border:1px solid #3c90d9;
 color: #fff;
 font-weight:700;
 /*background-color:#eee;*/
 background: #3c90d9 none repeat scroll 0 0;
}
#pager{margin-left: 290px;}

3.jquery.pager.js

/*
 * jQuery pager plugin
 * Version 1.0 (12/22/2008)
 * @requires jQuery v1.2.6 or later
 *
 * Example at: http://jonpauldavies.github.com/JQuery/Pager/PagerDemo.html
 *
 * Copyright (c) 2008-2009 Jon Paul Davies
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 * Read the related blog post and contact the author at http://www.j-dee.com/2008/12/22/jquery-pager-plugin/
 *
 * This version is far from perfect and doesn't manage it's own state, therefore contributions are more than welcome!
 *
 * Usage: .pager({ pagenumber: 1, pagecount: 15, buttonClickCallback: PagerClickTest });
 *
 * Where pagenumber is the visible page number
 *  pagecount is the total number of pages to display
 *  buttonClickCallback is the method to fire when a pager button is clicked.
 *
 * buttonClickCallback signiture is PagerClickTest = function(pageclickednumber)
 * Where pageclickednumber is the number of the page clicked in the control.
 *
 * The included Pager.CSS file is a dependancy but can obviously tweaked to your wishes
 * Tested in IE6 IE7 Firefox & Safari. Any browser strangeness, please report.
 */
(function($) {

 $.fn.pager = function(options) {

  var opts = $.extend({}, $.fn.pager.defaults, options);

  return this.each(function() {

   // empty out the destination element and then render out the pager with the supplied options
   $(this).empty().append(renderpager(parseInt(options.pagenumber), parseInt(options.pagecount), options.buttonClickCallback));

   // specify correct cursor activity
   $('.pages li').mouseover(function() { document.body.style.cursor = "pointer"; }).mouseout(function() { document.body.style.cursor = "auto"; });
  });
 };

 // render and return the pager with the supplied options
 function renderpager(pagenumber, pagecount, buttonClickCallback) {

  // setup $pager to hold render
  var $pager = $('<ul class="pages"></ul>');

  // add in the previous and next buttons
  //$pager.append(renderButton('first', pagenumber, pagecount, buttonClickCallback)).append(renderButton('prev', pagenumber, pagecount, buttonClickCallback));
  $pager.append(renderButton('上一页', pagenumber, pagecount, buttonClickCallback));
  // pager currently only handles 10 viewable pages ( could be easily parameterized, maybe in next version ) so handle edge cases
  var startPoint = 1;
  //var endPoint = 9;
  var endPoint = 5;
  if (pagenumber > 4 && pagenumber<pagecount-1) {
   startPoint = pagenumber - 2;
   endPoint = pagenumber + 2;
  }
  if(pagenumber>pagecount-2&& pagenumber<pagecount+1){
   startPoint = pagecount - 4;
   endPoint = pagecount;
  }
  if (endPoint > pagecount) {
   startPoint = pagecount - 8;
   endPoint = pagecount;
  }

  if (startPoint < 1) {
   startPoint = 1;
  }

  // loop thru visible pages and render buttons
  for (var page = startPoint; page <= endPoint; page++) {

   var currentButton = $('<li class="page-number">' + (page) + '</li>');

   page == pagenumber ? currentButton.addClass('pgCurrent') : currentButton.click(function() { buttonClickCallback(this.firstChild.data); });
   currentButton.appendTo($pager);
  }

  // render in the next and last buttons before returning the whole rendered control back.
  //$pager.append(renderButton('next', pagenumber, pagecount, buttonClickCallback)).append(renderButton('last', pagenumber, pagecount, buttonClickCallback));
  $pager.append(renderButton('下一页', pagenumber, pagecount, buttonClickCallback));
  return $pager;
 }

 // renders and returns a 'specialized' button, ie 'next', 'previous' etc. rather than a page number button
 function renderButton(buttonLabel, pagenumber, pagecount, buttonClickCallback) {

  var $Button = $('<li class="pgNext">' + buttonLabel + '</li>');

  var destPage = 1;

  // work out destination page for required button type
  switch (buttonLabel) {
   case "first":
    destPage = 1;
    break;
   case "上一页":
    destPage = pagenumber - 1;
    break;
   case "下一页":
    destPage = pagenumber + 1;
    break;
   case "last":
    destPage = pagecount;
    break;
  }

  // disable and 'grey' out buttons if not needed.
  if (buttonLabel == "first" || buttonLabel == "上一页") {
   pagenumber <= 1 ? $Button.addClass('pgEmpty') : $Button.click(function() { buttonClickCallback(destPage); });
  }
  else {
   pagenumber >= pagecount ? $Button.addClass('pgEmpty') : $Button.click(function() { buttonClickCallback(destPage); });
  }

  return $Button;
 }

 // pager defaults. hardly worth bothering with in this case but used as placeholder for expansion in the next version
 $.fn.pager.defaults = {
  pagenumber: 1,
  pagecount: 1
 };

})(jQuery);

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

jQuery 相关文章推荐
jQuery插件FusionCharts绘制的2D帕累托图效果示例【附demo源码】
Mar 28 jQuery
jQuery实现下拉菜单的实例代码
Jun 19 jQuery
jQuery实现base64前台加密解密功能详解
Aug 29 jQuery
jQuery选择器之属性筛选选择器用法详解
Sep 19 jQuery
jQuery扩展方法实现Form表单与Json互相转换的实例代码
Sep 05 jQuery
jQuery超简单遮罩层实现方法示例
Sep 06 jQuery
jQuery序列化form表单数据为JSON对象的实现方法
Sep 20 jQuery
jQuery动态操作表单示例【基于table表格】
Dec 06 jQuery
Jquery的autocomplete插件用法及参数讲解
Mar 12 jQuery
jQuery实现聊天对话框
Feb 08 jQuery
jQuery 移除事件的方法
Jun 20 jQuery
Jquery如何使用animation动画效果改变背景色的代码
Jul 20 jQuery
jQuery-Citys省市区三级菜单联动插件使用详解
Jul 26 #jQuery
jquery-ui 进度条功能示例【测试可用】
Jul 25 #jQuery
jquery ui 实现 tab标签功能示例【测试可用】
Jul 25 #jQuery
jQuery Ajax async=&gt;false异步改为同步时,解决导致浏览器假死的问题
Jul 22 #jQuery
jquery插件开发模式实例详解
Jul 20 #jQuery
jquery实现下载图片功能
Jul 18 #jQuery
jQuery实现图片下载代码
Jul 18 #jQuery
You might like
PHP警告Cannot use a scalar value as an array的解决方法
2012/01/11 PHP
PHP获取和操作配置文件php.ini的几个函数介绍
2013/06/24 PHP
PHP简单实现解析xml为数组的方法
2018/05/02 PHP
thinkphp5 框架结合plupload实现图片批量上传功能示例
2020/04/04 PHP
用Javascript实现锚点(Anchor)间平滑跳转
2009/09/08 Javascript
Jquery+WebService 校验账号是否已被注册的代码
2010/07/12 Javascript
jquery修改网页背景颜色通过css方法实现
2014/06/06 Javascript
JQuery设置时间段下拉选择实例
2014/12/30 Javascript
javascript实现可键盘控制的抽奖系统
2016/03/10 Javascript
基于React实现表单数据的添加和删除详解
2017/03/14 Javascript
vscode下vue项目中eslint的使用方法
2019/01/13 Javascript
详解vue-cli项目开发/生产环境代理实现跨域请求
2019/07/23 Javascript
解决vue.js提交数组时出现数组下标的问题
2019/11/05 Javascript
微信小程序接入vant Weapp组件的详细步骤
2020/10/28 Javascript
[02:35]DOTA2英雄基础教程 狙击手
2014/01/14 DOTA
python中getattr函数使用方法 getattr实现工厂模式
2014/01/20 Python
详解Python中find()方法的使用
2015/05/18 Python
详解python中的线程
2018/02/10 Python
python交换两个变量的值方法
2019/01/12 Python
基于Django统计博客文章阅读量
2019/10/29 Python
PyQt5+python3+pycharm开发环境配置教程
2020/03/24 Python
Pycharm配置PyQt5环境的教程
2020/04/02 Python
Python map及filter函数使用方法解析
2020/08/06 Python
HTML5 微格式和相关的属性名称
2010/02/10 HTML / CSS
HTML5本地存储之Database Storage应用介绍
2013/01/06 HTML / CSS
What is view? why do we have view?
2012/06/22 面试题
小学红领巾中秋节广播稿
2014/01/13 职场文书
一个大学生十年的职业规划
2014/01/17 职场文书
《三亚落日》教学反思
2014/04/26 职场文书
日语专业求职信
2014/07/04 职场文书
群众路线自查报告及整改措施
2014/11/04 职场文书
小学优秀教师材料
2014/12/15 职场文书
试用期工作表现自我评价
2015/03/06 职场文书
初中英语教学随笔
2015/08/15 职场文书
css3带你实现3D转换效果
2022/02/24 HTML / CSS
再谈python_tkinter弹出对话框创建
2022/03/20 Python