Well, while Cristiano G. Carvalho's answer works, I think it is overdoing things. The array()
+ the .each()
method will have a(n) (arguable) high cost in terms of memory and performance, much more if you have several images in your galleries.
I don't see any reason to iterate through every element and build the fancybox gallery (again) inside an array if a simple fancybox code and manual event handlers can tackle the issue. But that is entirely my personal opinion.
So for instance, if you have an html like this :
<h3>gallery one</h3>
<div id="gallery_one">
<a rel="galleryone" class="fancybox" href="http://fancyapps.com/fancybox/demo/1_b.jpg"><img src="http://fancyapps.com/fancybox/demo/1_s.jpg" alt=""/></a>
<a rel="galleryone" class="fancybox" href="http://fancyapps.com/fancybox/demo/2_b.jpg"><img src="http://fancyapps.com/fancybox/demo/2_s.jpg" alt=""/></a>
... etc.
</div>
<h3>gallery two</h3>
<div id="gallery_two">
<a rel="gallerytwo" class="fancybox" href="http://fancyapps.com/fancybox/demo/3_b.jpg"><img src="http://fancyapps.com/fancybox/demo/3_s.jpg" alt=""/></a>
<a rel="gallerytwo" class="fancybox" href="http://fancyapps.com/fancybox/demo/4_b.jpg"><img src="http://fancyapps.com/fancybox/demo/4_s.jpg" alt=""/></a>
...etc.
</div>
Rewriting my own answer to the same question, I would set any manual links I need like :
<a class="manualfancybox" data-gallery="gallery_one" href="#nogo">manual call to first gallery</a>
<a class="manualfancybox" data-gallery="gallery_two" href="#nogo">manual call to second gallery</a>
Notice that I added an HTML5 data-gallery
attribute that indicates what gallery the link is targeting to (the data-gallery
value matches the ID of the parent container of each gallery)
Then, add the fancybox code for all galleries
$(".fancybox").fancybox({
// API options here
});
and bind the event handler to the manual fancybox calls like :
$(".manualfancybox").on("click", function(){
var gallery = "#" + $(this).data("gallery");
$(gallery).find(".fancybox").eq(0).click();
return false;
});
Set the value of .eq()
depending on what image the gallery should start from.
See JSFIDDLE
NOTE : .on()
requires jQuery v1.7+
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…