Add randomizing function to fade-effect slideshow?

S

Susanna

Hi all,

I'm using the following slideshow script that I found on the web to
display changing images with a crossfade effect. Eventually I will be
adding many more images to the slideshow. The thing is, I ALSO have
to make it so the images will load randomly. I have looked at a
number of scripts for random-loading slideshows, but I have to find a
way to COMBINE this fading-image script (or, a different fading-image
script, if necessary) with a randomizing function. I haven't been
able to find a script online that does both. Unfortunately I don't
know how to code Javascript by hand and I haven't had any success
trying to hack this script myself to add the randomizing element. I
imagine that there is probably a fairly simple way to do it by adding
a line or two of code, but I don't know how to make it work...if
anyone can enlighten me, I'd be oh, so grateful.

Also, by the way: this crossfade slideshow only shows the fade effect
on PC's. Does anyone know of a script that makes this effect work on
the Mac too? (Also, on a Mac, this script does allow the images to
change, with no fade effect, BUT it is quite buggy and sometimes the
images start flashing around strangely...another problem. Any
ideas...?)

Thanks--
Susanna



Code:
In the header:

<script>
// (C) 2000 www.CodeLifter.com
// http://www.codelifter.com
// Free for all users, but leave in this header
// NS4-6,IE4-6
// Fade effect only in IE; degrades gracefully

// =======================================
// set the following variables
// =======================================

// Set slideShowSpeed (milliseconds)
var slideShowSpeed = 7000

// Duration of crossfade (seconds)
var crossFadeDuration = 4

// Specify the image files
var Pic = new Array() // don't touch this
// to add more images, just continue
// the pattern, adding to the array below

Pic[0] = 'slideshow/1.jpg'
Pic[1] = 'slideshow/2.jpg'
Pic[2] = 'slideshow/3.jpg'

// =======================================
// do not edit anything below this line
// =======================================

var t
var j = 0
var p = Pic.length

var preLoad = new Array()
for (i = 0; i < p; i++){
preLoad = new Image()
preLoad.src = Pic
}

function runSlideShow(){
if (document.all){
document.images.SlideShow.style.filter="blendTrans(duration=2)"
document.images.SlideShow.style.filter="blendTrans(duration=crossFadeDuration)"
document.images.SlideShow.filters.blendTrans.Apply()
}
document.images.SlideShow.src = preLoad[j].src
if (document.all){
document.images.SlideShow.filters.blendTrans.Play()
}
j = j + 1
if (j > (p-1)) j=0
t = setTimeout('runSlideShow()', slideShowSpeed)
}
</script>



In the body:

<td id="VU" height=72 width=160><img src="slideshow/1.jpg"
name='SlideShow' width="160" height="72"></td>
 
M

McKirahan

Susanna said:
Hi all,

I'm using the following slideshow script that I found on the web to
display changing images with a crossfade effect. Eventually I will be
adding many more images to the slideshow. The thing is, I ALSO have
to make it so the images will load randomly. I have looked at a
number of scripts for random-loading slideshows, but I have to find a
way to COMBINE this fading-image script (or, a different fading-image
script, if necessary) with a randomizing function. I haven't been
able to find a script online that does both. Unfortunately I don't
know how to code Javascript by hand and I haven't had any success
trying to hack this script myself to add the randomizing element. I
imagine that there is probably a fairly simple way to do it by adding
a line or two of code, but I don't know how to make it work...if
anyone can enlighten me, I'd be oh, so grateful.

Also, by the way: this crossfade slideshow only shows the fade effect
on PC's. Does anyone know of a script that makes this effect work on
the Mac too? (Also, on a Mac, this script does allow the images to
change, with no fade effect, BUT it is quite buggy and sometimes the
images start flashing around strangely...another problem. Any
ideas...?)

Thanks--
Susanna



Code:
In the header:

<script>
// (C) 2000 www.CodeLifter.com
// http://www.codelifter.com
// Free for all users, but leave in this header
// NS4-6,IE4-6
// Fade effect only in IE; degrades gracefully

// =======================================
// set the following variables
// =======================================

// Set slideShowSpeed (milliseconds)
var slideShowSpeed = 7000

// Duration of crossfade (seconds)
var crossFadeDuration = 4

// Specify the image files
var Pic = new Array() // don't touch this
// to add more images, just continue
// the pattern, adding to the array below

Pic[0] = 'slideshow/1.jpg'
Pic[1] = 'slideshow/2.jpg'
Pic[2] = 'slideshow/3.jpg'

// =======================================
// do not edit anything below this line
// =======================================

var t
var j = 0
var p = Pic.length

var preLoad = new Array()
for (i = 0; i < p; i++){
preLoad = new Image()
preLoad.src = Pic
}

function runSlideShow(){
if (document.all){
document.images.SlideShow.style.filter="blendTrans(duration=2)"
document.images.SlideShow.style.filter="blendTrans(duration=crossFadeDuratio
n)"
document.images.SlideShow.filters.blendTrans.Apply()
}
document.images.SlideShow.src = preLoad[j].src
if (document.all){
document.images.SlideShow.filters.blendTrans.Play()
}
j = j + 1
if (j > (p-1)) j=0
t = setTimeout('runSlideShow()', slideShowSpeed)
}
</script>



In the body:

<td id="VU" height=72 width=160><img src="slideshow/1.jpg"
name='SlideShow' width="160" height="72"></td>



Okay, here's an *ugly* solution -- I'm sure others will let me know just how
ugly.


Add the following above the comment "// do not edit anything below this
line":

// Populate rand() array
var pics = new Array(Pic.length);
var rand = new Array(Pic.length);
do {
for (var i=0; i<Pic.length; i++) {
var seed = parseInt(Math.random()*1000)%Pic.length;
if (rand == null) {
if (pics[seed] == null) {
rand = seed;
pics[seed] = i;
}
}
}
}
while (rand.join("").length < Pic.length);


And change one line below the comment "// do not edit anything below this
line":
preLoad.src = Pic
becomes:
preLoad.src = Pic[rand]


Note that with your code the first image displayed will always be
"slideshow/1.jpg".

Watch for word-wrap.
 
D

Dr John Stockton

JRS: In article <c4rEb.597098$Fm2.545352@attbi_s04>, seen in
news:comp.lang.javascript said:
var seed = parseInt(Math.random()*1000)%Pic.length;

No point in converting to a string and back; and you may have the wrong
result range; read the newsgroup FAQ, 4.22.

BTW, that is a case when parseInt does not need a second parameter. If
parseInt does not need a second parameter, then it is *probably* not
necessary to use parseInt.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top