Skip to content

Instantly share code, notes, and snippets.

@gleuch
Created November 30, 2013 17:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gleuch/7721755 to your computer and use it in GitHub Desktop.
Save gleuch/7721755 to your computer and use it in GitHub Desktop.
sample for random image replacement on page
// Really basic shuffle function for array, via http://stackoverflow.com/a/6274381
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