I guess you want this functionality for a random slideshow to run on the background or so.
Unfortunately javascript doesn't have an equivalent function to php's shuffle()
function. However, you could use any of javascript adapted Fisher-Yates algorithm for javascript to randomly sort your gallery.
Let's take this one from https://stackoverflow.com/a/6274398/1055987
function shuffle(array) {
// Fisher-Yates shuffle for javascript
// as in https://stackoverflow.com/a/6274398/1055987
var counter = array.length, temp, index;
// While there are elements in the array
while (counter > 0) {
// Pick a random index
index = Math.floor(Math.random() * counter);
// Decrease counter by 1
counter--;
// And swap the last element with it
temp = array[counter];
array[counter] = array[index];
array[index] = temp;
}
return array;
}
We will use the function above to randomize to our (manually constructed) gallery and serve it with fancybox, so no needed of any html but the fancybox trigger
<a class="fancybox" href="javascript:;">open random gallery</a>
Then the gallery will be stored inside an array like
var gallery = [{
href: "http://farm6.staticflickr.com/5471/9036958611_fa1bb7f827_b.jpg",
title: "Westfield Waterfalls - Middletown CT Lower (Graham_CS)"
}, {
href: "http://farm4.staticflickr.com/3824/9041440555_2175b32078_b.jpg",
title: "Calm Before The Storm (One Shoe Photography Ltd.)"
}, {
href: "http://farm3.staticflickr.com/2870/8985207189_01ea27882d_b.jpg",
title: "Lambs Valley (JMImagesonline.com)"
}, {
href: "http://farm4.staticflickr.com/3677/8962691008_7f489395c9_b.jpg",
title: "Grasmere Lake (Phil 'the link' Whittaker (gizto29))"
}];
... and we will randomize it every time we click our trigger ( the .fancybox
selector ) like :
jQuery(document).ready(function($) {
$(".fancybox").on("click", function () {
$.fancybox(shuffle(gallery), {
//API options
type: "image"
});
}); // on
}); // ready
See JSFIDDLE
NOTE: I am not sure if this is what you were looking for but I found the exercise interesting and eventually useful for other people applications.