Skip to content

Instantly share code, notes, and snippets.

@gleuch gleuch/gist:7721755
Created Nov 30, 2013

What would you like to do?
sample for random image replacement on page
// Really basic shuffle function for array, via
function shuffle(o){for(var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x); return o;};
// Your replacement images
var replaceImages = array('image1.jpg', 'image2.jpg'); // add more here
var replaceIndex = 0; // stores which image in your array you are on
// Randomize the array so it is not always the same each time
replaceImages = shuffle(replaceImages);
// Find all images on the site
jQuery('img').each(function() {
// this returns the remainder of a division, e.g. (12 % 5) == 2
var i = (replaceIndex % replaceImages.length);
// Replace image with yours
jQuery(this).attr('src', replaceImages[i]);
replaceIndex++; // up the index count
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.