One very awesome and useful plugin for jQuery is jQuery Cycle, which pretty much works out of the box, but browsing through the second part of the Intermediate Demos we’ve seen a click transition called Pager, which is somewhat tricky.
Pager is nothing but a few links next to your cycling block with page numbers, which switch the cycle upon click. It’s very useful when you have to create, say a list of services a company provides, with sweet lightboxes (don’t confuse with the Lightbox plugin), perhaps with some images, cycling through their descriptions, yet we’d like our visitors to be able to switch through the services manually, without having to wait. This is exactly what the Pager transition does, but hey, who the hell would want page numbers instead of their services names?
This is where pageAnchorBuilder comes in, and it’s not quite obvious what has to be done to make this work. According to the jQuery Cycle documentation it’s:
pageAnchorBuilder – callback function for building anchor links: function(index, DOMelement)
So here’s a demonstration of how this would work. Let’s setup a quick cycle, with a few options. Perhaps you’ll need more than I listed below ;)
jQuery('#fade').before('<div id="nav" class="nav"></div>').cycle({ pager: '#nav', pagerAnchorBuilder: paginate });
So paginate is a callback function which takes two arguments, the index and the DOM element. Using the index will work just fine. Let’s see how this works with text:
function paginate(ind, el) { if (ind == 0) return '<span>Service One</span>'; else if (ind == 1) return '<span>Service Two</span>'; // and so on }
I think that’s pretty straightforward once you understand how it works. Let’s try some images:
function paginate(ind, el) { return '<img src="http://whatever.com/services/service' + ind + '.jpg" />'; }
So name your images service0.jpg, service1.jpg, etc. And my favourite, CSS classes:
function paginate(ind, el) { return '<div class="service' + ind + '"></div>'; }
Then just add a few .service0, .service1, etc. classes in your stylesheet, voila! Hope that helps, and don’t forget about the other useful options. Cheers!
Social comments and analytics for this post…
This post was mentioned on Twitter by kovshenin: #jQuery Cycle: Pager and pagerAnchorBuilder https://konstantin.blog/2024 #webdesign…
[…] This post was mentioned on Twitter by kovshenin, kovshenin, colaja, Andrea Olivato, Naveen Lagadapati and others. Naveen Lagadapati said: #jQuery Cycle: Pager and pagerAnchorBuilder http://ow.ly/15udr #webdev #tech […]
Do you have a working demo of this custom text in the pager element? I can't seem to get your instructions to work
Thanks
The variable 'ind' isn't working anymore. Use 'idx' instead. (Version 2.99)
Thanks for the heads up!
Is there a way we can fade the pager in?
Fade the pager in? Could you please explain? :)