Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Random Backstretch image
<script>
// Create an array of images that you'd like to use
var images = [
'image1.jpg'
, 'image2.jpg'
, 'image3.jpg'
];
// Get a random number between 0 and the number of images
var randomNumber = Math.floor( Math.random() * images.length );
// Use the random number to load a random image
$.backstretch(images[randomNumber]);
</script>
@ghost

This comment has been minimized.

Copy link

commented Apr 8, 2013

Thank you! I used this and It helped out quite a bit!

@minethisis

This comment has been minimized.

Copy link

commented May 10, 2013

Thanks srobbin!
It worked perfectly for me.

@scperry19

This comment has been minimized.

Copy link

commented Aug 14, 2014

How would you do it if you have 1 single image that you want to appear a random number of multiple times? As in, click, randomly shows 1-5 of the same image?

@odera89

This comment has been minimized.

Copy link

commented Sep 2, 2014

Thanks.

@Siddharta11

This comment has been minimized.

Copy link

commented Sep 10, 2014

Thanks. I get a random image on every refresh. But the slidefunction doesn't work anymore.
With the code beneath I get a nice fade in, but there's no transition/ slide to the next image in the array.

 // Use the random number to load a random image
$.backstretch(images[randomNumber], {
            fade: 850,
            duration: 7000
        });

How can I get a slideshow with a randomly selected image at the start?

@ghost

This comment has been minimized.

Copy link

commented Sep 17, 2014

Here is how I made a slideshow from a directory of images with random start/order:

PHP code:


function getImagesFromDir($path) {
    $files = glob($path.'*.jpg');
    shuffle($files); 
    $images = "[";
    foreach ($files as $slide) {
        $images .= "'$slide',";
    }
    return substr($images,0,strlen($images)-1) . "]";
}

$.backstretch(, {duration: 3000, fade: 2000});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.