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()); |
22 | swPage.wrapAll( '<div class="swSlider" />' ); |
27 | var swSlider = ul.find( '.swSlider' ); |
28 | swSlider.append( '<div class="clear" />' ).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.
No comments:
Post a Comment