Created
November 30, 2013 17:18
-
-
Save gleuch/7721755 to your computer and use it in GitHub Desktop.
sample for random image replacement on page
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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