I have written this with the intention of randomising a background image each time a user visits a site. I had to use Ajax as the image is being used as a background image in css with some animations.
var images = [
'1.jpg', '2.jpg', '3.jpg', '4.jpg', '5.jpg', '6.jpg'];
var randomNumber = Math.floor(Math.random() * images.length);
var randomImage = "img/backgrounds/" + randomNumber + ".jpg";
jQuery.ajax(randomImage);
$(document).ajaxComplete(function() {
$('.cover').css({ 'background-image': 'url(' + randomImage + ')' }).addClass('loaded');
});
My problem is that some images seem to be used a lot more often that others and image number 6 never shows?
I was hoping someone who is more comfortable in Javascript might be able to shed some light on this for me.
Thanks in advance, Sam
Math.floor(Math.random() * (images.length + 1))