开发者

Hand off variable to html

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开发者_StackOverflow社区_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.


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' />


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.


I highly recommend jQuery and the Cycle plugin.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜