Random Background Image


/ Published in: JavaScript
Save to your folder(s)

Place this script inside your <BODY> tag. Make sure to change the number of images, image directory and the file names in the array.


Copy this code and paste it in your HTML
  1. <script type="text/javascript">
  2. // # of images
  3. var imgCount = 4;
  4. // image directory
  5. var dir = 'http://yourdomain.com/images/bg/';
  6. // random the images
  7. var randomCount = Math.round(Math.random() * (imgCount - 1)) + 1;
  8. // array of images & file name
  9. var images = new Array
  10. images[1] = "1.jpg",
  11. images[2] = "2.jpg",
  12. images[3] = "3.jpg",
  13. images[4] = "4.jpg";
  14. document.body.style.backgroundImage = "url(" + dir + images[randomCount] + ")";
  15. </script>

URL: http://marioluevanos.com/playground/Random%20Background/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.