Created by Martin Angelov on May 5th, 2010
![The Pagination Explained The Pagination Explained](https://lh3.googleusercontent.com/blogger_img_proxy/AEn0k_u5M6Kb2kTN6AP0q2JrCFDjfA1TU5Kf3PLDm_Z80mkxZUaDj52Mh1TOWP1Rgd7sLGe4DFJphrVhMvrOkCJJocyJ20E-O5a2JyusgpKxYMa8uYq7lY8AGBiWgPtzpQ=s0-d)
The Pagination Explained
Paginating content is a standard choice when dealing with large chunks of data. The implementation usually involves passing the page number to the back-end, where the appropriate data is fetched from the database and returned in some form. A cumbersome process, but it is a necessary evil. Or is it?
When dealing with small data sets, wouldn’t it be better to have the content readily available, but still neatly organized and easy to access?
Today we are making a jQuery plugin that will enable you to convert a regular unordered list of items into a SEO friendly set of easily navigatable pages. It can be used for comment threads, slideshows, or any kind of structured content.
The Idea
When called, the jQuery plugin splits the LI elements contained in the unordered list into a configurable number of groups. These groups (or pages) are floated to the left and hidden from view, as they overflow the UL which is given
overflow:hidden. A number of control links are generated, which slide the appropriate page of LIs into view.
You can also take a look at the illustration below.
![How it works How it works](https://lh3.googleusercontent.com/blogger_img_proxy/AEn0k_tXris74mxV3iBm3wEvn5YJVyfF9aHc0FEz-Ypf7QRi-MUJkn2rLai3AN7D3qSnpgPjtHye4a4h5nWEND3Ku2o0k51TtN5WWxksWTzhX786TOIDhv8aoQU7BqEO0Q=s0-d)
How it works
Step 1 – XHTML
The first step of the tutorial is to set up the XHTML markup. The plugin only needs an unordered list, UL, with some li elements inside it. Here is the code from demo.html, which you can find in the download archive:
demo.html
3 | < li >Lorem ipsum dolor sit amet...li > |
4 | < li >Lorem ipsum dolor sit amet...li > |
5 | < li >Lorem ipsum dolor sit amet...li > |
6 | < li >Lorem ipsum dolor sit amet...li > |
The main div acts as a container for the paginated UL, and is styled with a nice light-gray background. The unordered list holds the list elements (hence the id).
In most practical situations, the markup above would probably be generated by a back-end script, freeing you from having to do it manually. You could have all sorts of content inside those LIs, as the height and size is dynamically calculated by jQuery (just a reminder – if you plan on using images, specify the width and the height).
![A Pure jQuery & CSS Pagination A Pure jQuery & CSS Pagination](https://lh3.googleusercontent.com/blogger_img_proxy/AEn0k_vvukCkaP_H7pnTh7u8OoGFxKTtebn8Mzb9-QfwP1OGRGSsu_D_tNZHgMILgoXZcjjsfLCf54nqgWBMFSNN-l1jRoMl2peymC15g1E3d2YIAZXEmiHD9_H0k2qXcA=s0-d)
A Pure jQuery & CSS Pagination
Step 2 – CSS
After creating the XHTML markup, we can move on to styling it. If is a good idea to style your pages as if there were no navigation, as the plug-in is JavaScript dependent. This means that it is possible that some users will not be able to see nor use the pagination.
styles.css – Part 1
06 | background : url ( 'img/main_bg.jpg' ) repeat-x #aeadad ; |
07 | border : 1px solid #CCCCCC ; |
08 | padding : 70px 25px 60px ; |
12 | -moz-border-radius: 12px ; |
13 | -webkit-border-radius: 12px ; |
23 | background : url ( 'img/dark_bg.jpg' ) repeat #4e5355 ; |
29 | -moz-box-shadow: 0 0 10px #222 inset ; |
31 | box-shadow: 0 0 10px #222 inset ; |
First we style the main div and the unordered list (the latter is assigned the id of
holder).
Notice how we use the CSS3
box shadow property with the
inset attribute, to mimic an inner shadow. As with most CSS3 rules, we still have to provide vendor-specific prefixes for Mozilla (Firefox) and Webkit browsers (Safri and Chrome).
You can see that the webkit version of the property is commented out. This is because there is a bug in the rendering of box shadows in Chrome, when combined with the border-radius property (the shadows are rendered as if the div is square, ignoring the rounded corners and thus ruining the effect).
styles.css – Part 2
05 | background-color : #444444 ; |
15 | -moz-border-radius: 7px ; |
16 | -webkit-border-radius: 7px ; |
22 | background-color : #2993dd ; |
26 | -moz-box-shadow: 0 0 7px #1e435d inset ; |
28 | box-shadow: 0 0 7px #1e435d inset ; |
32 | background-color : #F4F4F4 ; |
33 | list-style : none outside none ; |
40 | -moz-box-shadow: 0 0 6px #111111 ; |
41 | -webkit-box-shadow: 0 0 6px #111111 ; |
42 | box-shadow: 0 0 6px #111111 ; |
48 | -moz-border-radius: 8px ; |
49 | -webkit-border-radius: 8px ; |
In the second part of the code, we style the page control links and the li elements. As you can see on line 46, we are applying rounded corners to both the unordered list and the li elements in one declaration, which saves us a from duplicating code.
Lastly is the clear class, which is used to clear the floats of the elements, also known as the
clearfix technique.
![The Pagination Explained The Pagination Explained](https://lh3.googleusercontent.com/blogger_img_proxy/AEn0k_u5M6Kb2kTN6AP0q2JrCFDjfA1TU5Kf3PLDm_Z80mkxZUaDj52Mh1TOWP1Rgd7sLGe4DFJphrVhMvrOkCJJocyJ20E-O5a2JyusgpKxYMa8uYq7lY8AGBiWgPtzpQ=s0-d)
The Pagination Explained
Step 3 – jQuery
Moving to the last part of the tutorial, we need to include the latest version of the jQuery library in the page. Performance-wise, it is best to include all external JavaScript files just before the closing body tag, as scripts block he rendering of the page.
script.js – Part 1
04 | $.fn.sweetPages = function (opts){ |
09 | var resultsPerPage = opts.perPage || 3; |
14 | var li = ul.find( 'li' ); |
21 | el.data( 'height' ,el.outerHeight( true )); |
25 | var pagesNumber = Math.ceil(li.length/resultsPerPage); |
28 | if (pagesNumber<2) return this ; |
31 | var swControls = $( ' '); |
36 | li.slice(i*resultsPerPage,(i+1)*resultsPerPage).wrapAll( ' '); |
42 | ul.append(swControls); |
Creating a jQuery plug-in is not as hard as you might think. We just need to create a new function as a property of
jQuery.fn (or
$.fn, as given here). The
this of the function points to the original jQuery object that it was called on.
Moving from there, we check for the existence of the
opts object and set
resultsPerPage accordingly. This is the number of li elements that are going to be grouped as a page.
After this, we calculate the total number of pages with the
Math.ceil() function. It rounds the result to the nearest greater integer, which gives the correct number of pages.
Now that we have the number of pages obtained, we can enter a for loop in which we split the li elements into portions and wrap them in a
swPage div, forming a page. Keep in mind that calling the jQuery
slice() method on line 36 creates a new set of elements and leaves the original set intact (thus in every iteration of the for loop we start with the original set of
li elements).
script.js – Part 2
04 | var swPage = ul.find( '.swPage' ); |
05 | swPage.each( function (){ |
12 | elem.find( 'li' ).each( function (){tmpHeight+=$( this ).data( 'height' );}); |
14 | if (tmpHeight>maxHeight) |
15 | maxHeight = tmpHeight; |
17 | totalWidth+=elem.outerWidth(); |
19 | elem.css( 'float' , 'left' ).width(ul.width()); |
27 | var swSlider = ul.find( '.swSlider' ); |
28 | swSlider.append( ' ').width(totalWidth); |
30 | var hyperLinks = ul.find( 'a.swShowPage' ); |
32 | hyperLinks.click( function (e){ |
37 | $( this ).addClass( 'active' ).siblings().removeClass( 'active' ); |
39 | swSlider.stop().animate({ 'margin-left' : -(parseInt($( this ).text())-1)*ul.width()}, 'slow' ); |
44 | hyperLinks.eq(0).addClass( 'active' ); |
49 | 'margin-left' :-swControls.width()/2 |
In the second part of the script, we loop through the newly created pages, set their sizes and float them to the left. In the process we also find the tallest page, and set the height of the ul accordingly.
We also wrap the pages inside a
swSlider div, which is wide enough to display them side by side. After this we listen for the click event on the control links, and slide the
swSlider div with the animate method. This creates the slide effect that, observed in the demo.
script.js – Part 3
01 | $(document).ready( function (){ |
07 | $( '#holder' ).sweetPages({perPage:3}); |
12 | var controls = $( '.swControls' ).detach(); |
13 | controls.appendTo( '#main' ); |
In the last part of the code, we just a call to the plugin and passing the perPage setting . Also notice the use of the detach method, introduced in jQuery 1.4. It removes elements from the DOM, but leaves all the event listeners intact. It enables us to move the controls outside of the UL they were originally inserted in, keeping the click functionality in place.
With this our sweet pagination solution with jQuery and CSS3 is complete!
Conclusion
With this plugin you can power any kinds of comment threads, slideshows, product pages or other kinds of data. The advantage is that if JavaScript is disabled you still end up with a semantic and SEO friendly code. However if you plan to display huge chunks of data, it is still best to implement a back-end solution, as with the plug-in all the content is transferred to the visitor’s browser
Postado por Fernando Schimit - Fox Creative