/    Sign up×
Community /Pin to ProfileBookmark

Fading Image Slideshow: random help.

Hi there,

I found this script here: [url]http://www.dynamicdrive.com/dynamicindex14/fadeinslideshow.htm[/url] … it’s the closest thing to what i’m looking for: a fading slideshow script that cycles randomly. This one, however, isn’t random. I’m not too good at Javascripting, could someone tell me how I’d turn this into a random script instead of cycling please?

Thanks!

[CODE]

var slideshow_width=’140px’ //SET IMAGE WIDTH
var slideshow_height=’225px’ //SET IMAGE HEIGHT
var pause=3000 //SET PAUSE BETWEEN SLIDE (3000=3 seconds)

var fadeimages=new Array()
//SET 1) IMAGE PATHS, 2) optional link, 3), optional link target:
fadeimages[0]=[“photo1.jpg”, “”, “”] //plain image syntax
fadeimages[1]=[“photo2.jpg”, “http://www.google.com”, “”] //image with link syntax
fadeimages[2]=[“photo3.jpg”, “http://www.javascriptkit.com”, “_new”] //image with link and target syntax

////NO need to edit beyond here/////////////

var preloadedimages=new Array()
for (p=0;p<fadeimages.length;p++){
preloadedimages[p]=new Image()
preloadedimages[p].src=fadeimages[p][0]
}

var ie4=document.all
var dom=document.getElementById

if (ie4||dom)
document.write(‘<div style=”position:relative;width:’+slideshow_width+’;height:’+slideshow_height+’;overflow:hidden”><div id=”canvas0″ style=”position:absolute;width:’+slideshow_width+’;height:’+slideshow_height+’;top:0;left:0;filter:alpha(opacity=10);-moz-opacity:10″></div><div id=”canvas1″ style=”position:absolute;width:’+slideshow_width+’;height:’+slideshow_height+’;top:0;left:0;filter:alpha(opacity=10);-moz-opacity:10;visibility: hidden”></div></div>’)
else
document.write(‘<img name=”defaultslide” src=”‘+fadeimages[0][0]+'”>’)

var curpos=10
var degree=10
var curcanvas=”canvas0″
var curimageindex=0
var nextimageindex=1

function fadepic(){
if (curpos<100){
curpos+=10
if (tempobj.filters)
tempobj.filters.alpha.opacity=curpos
else if (tempobj.style.MozOpacity)
tempobj.style.MozOpacity=curpos/101
}
else{
clearInterval(dropslide)
nextcanvas=(curcanvas==”canvas0″)? “canvas0” : “canvas1”
tempobj=ie4? eval(“document.all.”+nextcanvas) : document.getElementById(nextcanvas)
tempobj.innerHTML=insertimage(nextimageindex)
nextimageindex=(nextimageindex<fadeimages.length-1)? nextimageindex+1 : 0
var tempobj2=ie4? eval(“document.all.”+nextcanvas) : document.getElementById(nextcanvas)
tempobj2.style.visibility=”hidden”
setTimeout(“rotateimage()”,pause)
}
}

function insertimage(i){
var tempcontainer=fadeimages[i][1]!=””? ‘<a href=”‘+fadeimages[i][1]+'” target=”‘+fadeimages[i][2]+'”>’ : “”
tempcontainer+='<img src=”‘+fadeimages[i][0]+'” border=”0″>’
tempcontainer=fadeimages[i][1]!=””? tempcontainer+'</a>’ : tempcontainer
return tempcontainer
}

function rotateimage(){
if (ie4||dom){
resetit(curcanvas)
var crossobj=tempobj=ie4? eval(“document.all.”+curcanvas) : document.getElementById(curcanvas)
crossobj.style.zIndex++
tempobj.style.visibility=”visible”
var temp=’setInterval(“fadepic()”,50)’
dropslide=eval(temp)
curcanvas=(curcanvas==”canvas0″)? “canvas1” : “canvas0”
}
else
document.images.defaultslide.src=fadeimages[curimageindex][0]
curimageindex=(curimageindex<fadeimages.length-1)? curimageindex+1 : 0
}

function resetit(what){
curpos=10
var crossobj=ie4? eval(“document.all.”+what) : document.getElementById(what)
if (crossobj.filters)
crossobj.filters.alpha.opacity=curpos
else if (crossobj.style.MozOpacity)
crossobj.style.MozOpacity=curpos/101
}

function startit(){
var crossobj=ie4? eval(“document.all.”+curcanvas) : document.getElementById(curcanvas)
crossobj.innerHTML=insertimage(curimageindex)
rotateimage()
}

if (ie4||dom)
window.onload=startit
else
setInterval(“rotateimage()”,pause)

</script>
[/CODE]

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@UltimaterMay 08.2005 — The original writter is an amateur! :p

The function eval should be avoided whenever possible!

replace:
eval("document.all."+what)
with:
document.all[what]
and replace:
eval("document.all."+curcanvas)
with:
<i>
</i>document.all[curcanvas]

For each instance do the same. That would make a [i]better[/i] original copy and be closer to what the dude should have wrote. He also left NS out in the blue...

So what exactly did you have in-mind when you said you wanted it to be random?

Did you want it to randomize once and continue to that pattern?

Did you want it to flip-flop from random image to random image?
Copy linkTweet thisAlerts:
@knappsterauthorMay 08.2005 — Thanks! ? ... Really anything! If just plain ol' random is easier, that'd be great.
Copy linkTweet thisAlerts:
@UltimaterMay 08.2005 — replace the current images with these:
<i>
</i>fadeimages[0]=["http://www.webdeveloper.com/forum/image.php?u=30185&amp;dateline=1109700903", "", ""] //plain image syntax
fadeimages[1]=["http://www.webdeveloper.com/forum/image.php?u=22393&amp;dateline=1090774426", "http://www.google.com", ""] //image with link syntax
fadeimages[2]=["http://www.webdeveloper.com/forum/image.php?u=30740&amp;dateline=1115558351", "http://www.javascriptkit.com", "_new"] //image with link and target syntax
var temp_r=[-1,1]
fadeimages.sort(function(){return temp_r[Math.floor(Math.random()*2)]})


then add the following to the very top of the rotateimage() function:
<i>
</i>var temp_r=[-1,1]
fadeimages.sort(function(){return temp_r[Math.floor(Math.random()*2)]})
Copy linkTweet thisAlerts:
@knappsterauthorMay 08.2005 — Woohoo! Works great. Thanks so much!
Copy linkTweet thisAlerts:
@UltimaterMay 08.2005 — Happy to help! Feel free to add an entry into my guestbook in return.

(refer to signature)
×

Success!

Help @knappster spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...