Question

Hello I have a folder with a few images, and im using FancyBox to show them in a gallery. But puting one by one takes too much time and I have to go back to the code to put another picture. I was wondering if anyone knew a JS that could put in a FancyBox Gallery all the images in a certain folder to be put automatically. Right now Im doing this:

<div class="selCuad" id="Reg"><a class="fancybox-thumb aMenu" rel="fancybox-thumb" href="http://farm9.staticflickr.com/8507/8454547519_f8116520e1_b.jpg" title="Ayvalık, Turkey (Nejdet Düzen)">GALERÍA</a>
        <a class="fancybox-thumb" rel="fancybox-thumb" href="http://farm8.staticflickr.com/7152/6394238505_c94fdd1d89_b.jpg" title="Sicilian Scratches   erice (italianoadoravel on/off coming back)"></a>
        <a class="fancybox-thumb" rel="fancybox-thumb" href="http://farm9.staticflickr.com/8481/8215602321_69d9939b8b_b.jpg" title="The Trail (Msjunior-Check out my galleries)"></a>
        <a class="fancybox-thumb" rel="fancybox-thumb" href="http://farm9.staticflickr.com/8200/8220393833_e52cabfe80_b.jpg" title="Trees (Joerg Marx)"></a>
        </div>

I want to put maybe one anchore and let a JS call the rest. Could this be made? Im searching on my side also. If I find a answer Ill post it here :D

Was it helpful?

Solution

If you're willing to use some javascript and constrain yourself within certain limitations, you could make a helper function that can speed up the process for you.

Fancybox API allows you to call a function to generate a new gallery on the fly:

$.fancybox( [A], {B});

Where [A] is an array of objects, each one specifying a content object (and the order in wich they appear in the fancybox presentation will correspond with their index in the array); and {B} is an options object containing the customisations you want for this instance of fancybox.

The content objects can have multiple optional properties that fancybox uses to enrich the presentation, such as a title for the image, but in this case we only need the address path, leaving you with e.g:

var content = {
    href: "path/to/image.jpg"
};

If you have your images in a known location and named in secuential numeric order, you can make a function that fills an array with all the individual locations of this images to pass to the $.fancybox( [A], {B}) function:

//remember that your first image must have "0" as number!!

function createPaths(prefix, amount, sufix) {
    var addresses = [];

    for(var i = 0; i < amount; i++) {
        var thisAddress = prefix + i + sufix;
        addresses[i] = { href: thisAddress };
    }

    return addresses;
}

Then you call this function for each whole presentation, eg:

//This variable will store 124 objects with
// the images' paths (path/to/0.jpg, path/to/1.jpg ... path/to/123.jpg)
var allImages_gallery_1 = createPaths("path/to/" , 124, ".jpg");

And finally, pass this array to $.fancybox();, but put it inside another function, like this:

function open_gallery_1() {
    $.fancybox( allImages_gallery_1, optionsObject );
}

Then, in your HTML you can make anything call this function with the onclick="" attribute, eg:

<img src="image.jpg" onclick="open_gallery_1();">
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top