Last active
October 12, 2017 05:18
-
-
Save AstDerek/5841966 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Random Image</title> | |
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script> | |
<script> | |
(function($){ | |
$.randomImage = { | |
defaults: { | |
//you can change these defaults to your own preferences. | |
path: 'http://mysite.com/images/', //change this to the path of your images | |
myImages: ['Testimonial1.png', 'Testimonial2.png', 'Testimonial3.png', 'Testimonial4.png', 'Testimonial5.png' ] //put image names in this bracket. ex: 'harold.jpg', 'maude.jpg', 'etc' | |
} | |
}; | |
$.fn.extend({ | |
randomImage:function(config) { | |
var config = $.extend({}, $.randomImage.defaults, config); | |
return this.each(function() { | |
var imageNames = config.myImages, | |
//get size of array, randomize a number from this | |
// use this number as the array index | |
imageNamesSize = imageNames.length, | |
lotteryNumber = Math.floor(Math.random()*imageNamesSize), | |
winnerImage = imageNames[lotteryNumber], | |
fullPath = config.path + winnerImage; | |
//put this image into DOM at class of randomImage | |
// alt tag will be image filename. | |
$(this).attr({ | |
src: fullPath, | |
alt: winnerImage | |
}); | |
}); | |
} | |
}); | |
}(jQuery)); | |
$(document).ready(function(){ | |
$('img:first').randomImage(); | |
}); | |
</script> | |
</head> | |
<body> | |
<img> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi AstDerek, Would you care to help me out please? At http://www.thijsmagielse.nl/nieuw.shtml I am trying to replace the long grey image by a random picture. I can't find out how to id your script to the place on my page where I want the random image to appear.
Kind regards, Wim