mardi 6 novembre 2018

Javascript Array - getting random photo to display when browser loads

Free Code Camp Project

I am working on a tribute page project, and have decided to add a javascript random photo array to the project. This is working fine, but I cant figure out how to get a random photo to appear whenever the browser reloads.

here is my html

<div id="albumCovers"></div>
<button id='randomBut'>View Random Album Covers</button>
<div><img id='movieImg'></div>

here is the javascript

var randomizer = 
document.getElementById("randomBut");
var randImg = 
document.getElementById("movieImg");
var movieImages = ["all.jpg", 
"dontgrowup.jpg",
"everythingSux.jpg",
"enjoy.jpg",
"college.jpg",
"hyper.jpg",
"cooltobeyou.jpg"];

randomizer.addEventListener("click", function() 
{
var randNum = Math.floor(Math.random() * 
movieImages.length) + 0  

randImg.src = movieImages[randNum] ;
randomizer.click();
});

any suggestions?




Aucun commentaire:

Enregistrer un commentaire