PDA

View Full Version : Ultimate Fade-In Slideshow - limiting number of random slides



deGama
10-18-2012, 11:35 AM
1) Script Title: Ultimate Fade-In slide show

2) Script URL (on DD): http://www.dynamicdrive.com/dynamicindex14/fadeinslideshow.htm

3) Describe problem:

Hello. Great script. Works brilliantly, however, I was just wondering if anyone had produced a solution whereby a limited number of random slides are selected for display?

For example,


imagearray: [
["image1.jpg", "http://site1.com"],
["image2.jpg", "http://site2.com"],
["image3.jpg", "http://site3.com"],
["image4.jpg", "http://site4.com"],
["image5.jpg", "http://site5.com"],
["image6.jpg", "http://site6.com"],
["image7.jpg", "http://site7.com"]
],

But just select 4 slides from the 7 to display.

It seems easy enough to select random elements to insert in the imagearray[], aside from adding no trailing , to the final element.

Many thanks!

jscheuer1
10-18-2012, 02:52 PM
Just add the highlighted:


imagearray: [
["image1.jpg", "http://site1.com"],
["image2.jpg", "http://site2.com"],
["image3.jpg", "http://site3.com"],
["image4.jpg", "http://site4.com"],
["image5.jpg", "http://site5.com"],
["image6.jpg", "http://site6.com"],
["image7.jpg", "http://site7.com"]
].sort(function(){return 0.5 - Math.random();}).slice(0, 4),

The browser cache may need to be cleared and/or the page refreshed to see changes.

If you want more help, please include a link to the page on your site that contains the problematic code so we can check it out.

vwphillips
10-18-2012, 02:53 PM
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title></title>
</head>

<body>
<script type="text/javascript">

var A1= [
["image1.jpg", "http://site1.com"],
["image2.jpg", "http://site2.com"],
["image3.jpg", "http://site3.com"],
["image4.jpg", "http://site4.com"],
["image5.jpg", "http://site5.com"],
["image6.jpg", "http://site6.com"],
["image7.jpg", "http://site7.com"]
]

function shuffle(ary){
for (var r,t,z0=0;z0<ary.length;z0++){
r=Math.floor(Math.random()*ary.length);
t=ary[z0];
ary[z0]=ary[r];
ary[r]=t;
}
return ary;
}

A1=shuffle(A1);

A1.length=4;

alert(A1.join('\n'));

//then use
//imagearray:A1,

</script>
</body>

</html>

jscheuer1
10-18-2012, 03:06 PM
Mine is simpler:


imagearray: [
["image1.jpg", "http://site1.com"],
["image2.jpg", "http://site2.com"],
["image3.jpg", "http://site3.com"],
["image4.jpg", "http://site4.com"],
["image5.jpg", "http://site5.com"],
["image6.jpg", "http://site6.com"],
["image7.jpg", "http://site7.com"]
].sort(function(){return 0.5 - Math.random();}).slice(0, 4),

deGama
10-18-2012, 07:25 PM
Many thanks, both of you, for your help. The solution works a treat.

Thanks again.