Typically, fancybox displays items in the gallery based on the order they are added in the HTML code.
Is there a way to customize the order of items when they are opened in the popup, while keeping the original order when displayed on the page?
The solution involves adding a optional data-
attribute to each item:
<a class="fancybox" rel="gallery1" href="http://farm2.staticflickr.com/1617/24108587812_6c9825d0da_b.jpg" title="Morning Godafoss (Brads5)">
<img src="http://farm2.staticflickr.com/1617/24108587812_6c9825d0da_m.jpg" alt="" />
</a>
<a class="fancybox" rel="gallery1" href="http://farm4.staticflickr.com/3691/10185053775_701272da37_b.jpg" title="Vertical - Special Edition! (cedarsphoto)">
<img src="http://farm4.staticflickr.com/3691/10185053775_701272da37_m.jpg" alt="" />
</a>
<a class="fancybox" rel="gallery1" href="http://farm1.staticflickr.com/574/22407305427_69cc6e845f_b.jpg" title="Racing against the Protons (tom.leuzi)">
<img src="http://farm1.staticflickr.com/574/22407305427_69cc6e845f_m.jpg" alt="" />
</a>
<a class="fancybox" rel="gallery1" href="http://farm1.staticflickr.com/291/18653638823_a86b58523c_b.jpg" title="Lupines (Kiddi Einars)">
<img src="http://farm1.staticflickr.com/291/18653638823_a86b58523c_m.jpg" alt="" />
</a>
JavaScript code for fancybox initialization:
$(".fancybox").fancybox();
If you want to open the images in the popup in a different order than they appear on the page, you can use the following setup:
Specify the desired order using the data-fancybox-order
attribute like this:
<a class="fancybox" rel="gallery1" data-fancybox-order="3" ... >
<a class="fancybox" rel="gallery1" data-fancybox-order="1" ... >
<a class="fancybox" rel="gallery1" data-fancybox-order="4" ... >
<a class="fancybox" rel="gallery1" data-fancybox-order="2" ... >
With this setup, navigation buttons will adjust based on the specified order. For example, if the second image is clicked (data-fancybox-order="1"
), there will be no previous button. Clicking next will open the fourth image, then the first, and finally the third image (data-fancybox-order="4"
), after which the next button will disappear.
What's the best approach to implement this?