Pagination component based on jQuery encapsulation
Foreword:
Since the project needed to achieve the paging effect, I searched in the jQuery plug-in library, but I couldn’t find the effect I wanted, so I encapsulated a paging component myself.
Idea:
It is mainly based on the paging template established based on the prototype during initialization and then binds dynamic events to achieve the paging effect of refreshing the DOM.
1.page.init.css
1 @charset "utf=8"; 2 *{ 3 box-sizing: border-box; 4 padding: 0; 5 margin: 0; 6 } 7 .page{ 8 font-size: 13px; 9 text-align: right;10 }11 .page .page_to{12 display: inline-block;13 max-width: 250px;14 }15 .page .page_to li{16 display: inline-block;17 width: auto;18 height: auto;19 border: 1px solid #ddd;20 padding:5px 10px;21 border-left-width: 0;22 color: #323232;23 cursor: pointer;24 }25 .page .page_to li.page_hide{26 display: none;27 }28 .page .page_to li:hover{29 color: #32C2CD;30 background-color: #f4f4f4;31 border-color: #DDDDDD;32 }33 .page .page_to li:first-child{34 border-left-width: 1px;35 }36 .page .page_jump{37 display: inline-block;38 width: 180px;39 }40 .page .page_jump input.page_jump_input{41 width: 52px;42 height: 28px;43 text-align: center;44 text-decoration: none;45 background-color: #fff;46 border: 1px solid #ddd;47 margin:0 4px;48 }49 .page .page_jump input.page_jump_btn{50 display: inline-block;51 padding: 7px 20px;52 margin-left: 5px;53 font-size: 14px;54 font-weight: 400;55 line-height: 1.42857143;56 text-align: center;57 white-space: nowrap;58 vertical-align: middle;59 -ms-touch-action: manipulation;60 touch-action: manipulation;61 cursor: pointer;62 -webkit-user-select: none;63 -moz-user-select: none;64 -ms-user-select: none;65 user-select: none;66 border: 1px solid transparent;67 border-radius: 4px;68 background-color: #00BB9C;69 color: #FFFFFF;70 font-weight: bold;71 }
2.pageInit.js
1 /** 2 * Created: 2017/6/20. 3 * author: Aaron 4 * address: 5 */ 6 (function($,window,undefined){ 7 8 var curPage='', 9 //跳转是否有值 10 jumpVal='', 11 //从DOM中重新获取数据总数/总页数 12 lists='', 13 totals='', 14 //是否返回值 15 isTrue=false; 16 17 var Page=function(opts){ 18 this.settings= $.extend({},Page.defaults,opts); 19 curPage=this.settings.initPage; 20 totals=this.settings.totalPages; 21 jumpVal=this.settings.inputVal; 22 this.init(); 23 }; 24 25 //默认配置 26 Page.defaults={ 27 container:'.page', 28 setPos:'body', 29 totalPages:null, 30 totalLists:null, 31 initPage:1, 32 inputVal:1, 33 callBack:null 34 }; 35 36 Page.prototype={ 37 init:function(){ 38 this.create(); 39 }, 40 create:function(){ 41 var _template='<div class="page">'+ 42 '<span class="page_details">'+ 43 '共<span class="page_num">'+this.settings.totalLists+'</span>条记录,'+ 44 '第<span class="page_current">'+curPage+'</span>/'+ 45 '<span class="page_size">'+this.settings.totalPages+'</span>页'+ 46 '</span>'+ 47 '<div class="page_to">'+ 48 '<ul class="flex_parent">'+ 49 '<li class="page_first flex_child">首页</li>'+ 50 '<li class="page_pre page_hide flex_child">« 上一页</li>'+ 51 '<li class="page_next flex_child">下一页 »</li>'+ 52 '<li class="page_last flex_child">末页</li>'+ 53 '</ul>'+ 54 '</div>'+ 55 '<div class="page_jump">'+ 56 '<span>第:<input type="number" class="page_jump_input" value="'+this.settings.inputVal+'">页</span>'+ 57 '<input type="button" class="page_jump_btn" value="Go">'+ 58 '</div>'+ 59 '</div>'; 60 $(this.settings.setPos).append(_template); 61 this.refreshDom(); 62 this.bindEvent(); 63 }, 64 bindEvent:function(){ 65 var _this=this; 66 //跳转首页 67 $(this.settings.container).on("click",".page_first",function(){ 68 69 lists=$(_this.settings.container).find(".page_num").text(); 70 totals=$(_this.settings.container).find(".page_size").text(); 71 72 if($.isFunction(_this.settings.callBack)){ 73 curPage=1; 74 isTrue=_this.settings.callBack(1); 75 if(isTrue){ 76 _this.refreshDom(); 77 $(_this.settings.container).find(".page_current").text(1); 78 $(_this.settings.container).find(".page_jump_input").val(curPage); 79 } 80 } 81 }); 82 //跳转上一页 83 $(this.settings.container).on("click",".page_pre",function(){ 84 85 lists=$(_this.settings.container).find(".page_num").text(); 86 totals=$(_this.settings.container).find(".page_size").text(); 87 88 if($.isFunction(_this.settings.callBack)){ 89 if(curPage>1){ 90 curPage=curPage-1; 91 isTrue=_this.settings.callBack(curPage); 92 if(isTrue){ 93 _this.refreshDom(); 94 $(_this.settings.container).find(".page_current").text(curPage); 95 $(_this.settings.container).find(".page_jump_input").val(curPage); 96 } 97 } 98 } 99 });100 //跳转下一页101 $(this.settings.container).on("click",".page_next",function(){102 103 lists=$(_this.settings.container).find(".page_num").text();104 totals=$(_this.settings.container).find(".page_size").text();105 106 107 if($.isFunction(_this.settings.callBack)){108 if(curPage<totals){109 curPage=curPage+1;110 isTrue=_this.settings.callBack(curPage);111 if(isTrue){112 _this.refreshDom();113 $(_this.settings.container).find(".page_current").text(curPage);114 $(_this.settings.container).find(".page_jump_input").val(curPage);115 }116 }117 }118 });119 //跳转末页120 $(this.settings.container).on("click",".page_last",function(){121 122 lists=$(_this.settings.container).find(".page_num").text();123 totals=$(_this.settings.container).find(".page_size").text();124 125 if($.isFunction(_this.settings.callBack)){126 curPage=totals;127 isTrue=_this.settings.callBack(curPage);128 if(isTrue){129 _this.refreshDom();130 $(_this.settings.container).find(".page_current").text(totals);131 $(_this.settings.container).find(".page_jump_input").val(curPage);132 }133 }134 });135 //Go跳转136 $(this.settings.container).on("click",".page_jump_btn",function(){137 138 lists=$(_this.settings.container).find(".page_num").text();139 totals=$(_this.settings.container).find(".page_size").text();140 141 if($.isFunction(_this.settings.callBack)){142 jumpVal=Number($(_this.settings.container).find("input.page_jump_input").val());143 console.log('跳转的页数:'+jumpVal+';跳转之前的页数:'+curPage);144 if(jumpVal>=1 && jumpVal <=totals){145 curPage=jumpVal;146 isTrue=_this.settings.callBack(curPage);147 if(isTrue){148 _this.refreshDom();149 $(_this.settings.container).find(".page_current").text(curPage);150 }151 }else{152 jumpVal=curPage;153 }154 }155 });156 },157 refreshDom:function(){158 $(this.settings.container).find("li.flex_child").removeClass("page_hide");159 if(Number(totals)==1){160 $(this.settings.container).find(".page_pre").addClass("page_hide");161 $(this.settings.container).find(".page_next").addClass("page_hide");162 }163 else if(Number(totals)==2){164 if(Number(curPage)==1){165 $(this.settings.container).find(".page_pre").addClass("page_hide");166 }else{167 $(this.settings.container).find(".page_next").addClass("page_hide");168 }169 }170 else if(Number(curPage)==1 && Number(totals)>2){171 $(this.settings.container).find(".page_pre").addClass("page_hide");172 }173 else if(Number(curPage)==Number(totals) && Number(totals)>2){174 $(this.settings.container).find(".page_next").addClass("page_hide");175 }176 }177 };178 179 var pageInit=function(opts){180 return new Page(opts);181 };182 183 window.pageInit= $.pageInit=pageInit;184 185 })(jQuery,window,undefined);
3.Component call
Paging component initialization can be completed through window.pageInit= $.pageInit=pageInit.
The exposed interfaces are:
1.container: DOM container, default.page
2.setPos: HTML position placed by DOM, default body
3.totalPages: Default number of pages, required, default null
4.totalLists: Default total number of data, required, default null
5.initPage: Current page ,Default first page
6.inputVal: Jump page, default first page
7.callBack: Executed callback function, required, default null
1 <!DOCTYPE html> 2 <html> 3 <head lang="en"> 4 <meta charset="UTF-8"> 5 <title>基于jQuery封装的分页组件</title> 6 <link rel="stylesheet" href="css/page.init.css?1.1.11"> 7 </head> 8 <body> 9 <script src="https://cdn.bootcss.com/jquery/2.2.4/jquery.js?1.1.11"></script>10 <script src="js/pageInit.js?1.1.11"></script>11 <script>12 $.pageInit(13 {14 container:'.page',//容器:默认page15 //setPos:'body',//放置位置:默认body16 totalPages:10,//总页数:必填17 totalLists:100,//数据总数:必填18 initPage:1,//初始页码:默认119 inputVal:1,//设置跳转的input值:默认120 //要执行的函数:默认null,必须为fn且返回true则可执行分页,false则不执行21 callBack:function(n){22 var flag=true;23 console.log(n);24 return flag;25 }26 }27 );28 </script>29 </body>30 </html>
Effect:
Add the function function you need to execute through the callBack interface, and only perform dynamic DOM rendering when true is required.
The above is the detailed content of Pagination component based on jQuery encapsulation. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The default display behavior for components in the Angular framework is not for block-level elements. This design choice promotes encapsulation of component styles and encourages developers to consciously define how each component is displayed. By explicitly setting the CSS property display, the display of Angular components can be fully controlled to achieve the desired layout and responsiveness.

How to remove the height attribute of an element with jQuery? In front-end development, we often encounter the need to manipulate the height attributes of elements. Sometimes, we may need to dynamically change the height of an element, and sometimes we need to remove the height attribute of an element. This article will introduce how to use jQuery to remove the height attribute of an element and provide specific code examples. Before using jQuery to operate the height attribute, we first need to understand the height attribute in CSS. The height attribute is used to set the height of an element

How to use PUT request method in jQuery? In jQuery, the method of sending a PUT request is similar to sending other types of requests, but you need to pay attention to some details and parameter settings. PUT requests are typically used to update resources, such as updating data in a database or updating files on the server. The following is a specific code example using the PUT request method in jQuery. First, make sure you include the jQuery library file, then you can send a PUT request via: $.ajax({u

Title: jQuery Tips: Quickly modify the text of all a tags on the page In web development, we often need to modify and operate elements on the page. When using jQuery, sometimes you need to modify the text content of all a tags in the page at once, which can save time and energy. The following will introduce how to use jQuery to quickly modify the text of all a tags on the page, and give specific code examples. First, we need to introduce the jQuery library file and ensure that the following code is introduced into the page: <

Title: Use jQuery to modify the text content of all a tags. jQuery is a popular JavaScript library that is widely used to handle DOM operations. In web development, we often encounter the need to modify the text content of the link tag (a tag) on the page. This article will explain how to use jQuery to achieve this goal, and provide specific code examples. First, we need to introduce the jQuery library into the page. Add the following code in the HTML file:

There are two most common ways to paginate PHP arrays: using the array_slice() function: calculate the number of elements to skip, and then extract the specified range of elements. Use built-in iterators: implement the Iterator interface, and the rewind(), key(), current(), next(), and valid() methods are used to traverse elements within the specified range.

How to tell if a jQuery element has a specific attribute? When using jQuery to operate DOM elements, you often encounter situations where you need to determine whether an element has a specific attribute. In this case, we can easily implement this function with the help of the methods provided by jQuery. The following will introduce two commonly used methods to determine whether a jQuery element has specific attributes, and attach specific code examples. Method 1: Use the attr() method and typeof operator // to determine whether the element has a specific attribute

jQuery is a popular JavaScript library that is widely used to handle DOM manipulation and event handling in web pages. In jQuery, the eq() method is used to select elements at a specified index position. The specific usage and application scenarios are as follows. In jQuery, the eq() method selects the element at a specified index position. Index positions start counting from 0, i.e. the index of the first element is 0, the index of the second element is 1, and so on. The syntax of the eq() method is as follows: $("s
