views:

49

answers:

3

So instead paying $35 for slideshowpro for lightroom, I just decided to whip up a quick javascript slideshow to do a simple task. Rotate 10 images within a div, randomly. It works like so:

var imageSrc = "source_folder/";
var imageType = ".jpg";
var randomImage = imageSrc + 0 + Math.floor(#*Math.random()) + imageType;

(This isn't all the code, I've left out the rest)

How do I take randomImage and insert into an <img> tag.

src="randomImage" will not work.
A: 

Don't use the string "randomImage", use the variable:

image.src = randomImage

This is just setting the src to the path of the current image. There will be a delay as the images are loading. You could preload paths using new Image().src = [path], or use image sprites and css clip to make it more seamless.

Jason Harwig
So, image.src = randomImage goes into the <img/> tag itself?
Francis
A: 

I highly recommend jQuery and the Cycle plugin.

Jeff
+2  A: 
var imageSrc = "source_folder/";
var imageType = ".jpg";
var randomImage = imageSrc + 0 + Math.floor(#*Math.random()) + imageType;
document.getElementById('booba').src=randomImage;

....
....
....

<img id='booba' />

And if you want to be noty (not a good practice, but should work)

<img src='javascript:this.src=randomImage' />
Itay Moav
Wouldn't work for me, I just get a box with an image-not-loaded box.
Francis
please alert the `randomImage` before `document.getElementById('booba').src=randomImage;`And this will show you why. Or simply right click on the image->properties
Itay Moav
Still nothing, the alert looks fine. I had to remove the + 0 from the randomImage, but it would alert out to something like source_folder/1.jpg (which does exist in the directory) but the <img> tag (which is located correctly) will not display anything in the div.
Francis
You have something wrong in either the path you use, ot the image name. If the src of the image gets a value, then this is done. Now you have to find out what is wrong with this value. I would copy the alerted string and past it manualy into the src attribute of an image in the same file/page, and see if it is loaded.
Itay Moav