Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Modifying a Slide Show js script?

Modifying a Slide Show js script?
Thread Tools
Mac Elite
Join Date: May 2002
Status: Offline
Reply With Quote
Feb 23, 2005, 08:17 AM
 
Hi,

I'm looking to modify an old Slide Show script I use to use to accept more than one array and feed to the script which array of images to use when cycling forward or backward.

This is what I was using:

//SLIDE SHOWS
myPix = new Array("../Images/Publishing/Candy_440-475_Sample1.jpg","../Images/Publishing/Candy_440-475_Sample2.jpg","../Images/Publishing/Candy_440-475_Sample3.jpg","../Images/Publishing/Candy_440-475_Sample4.jpg","../Images/Publishing/Candy_440-475_Sample5.jpg","../Images/Publishing/Candy_440-475_Sample6.jpg")

thisPic = 0
imgCt = myPix.length - 1

function chgSlide(direction) {
if(document.images) {
thisPic = thisPic + direction
if (thisPic > imgCt) {
thisPic = 0
}
if (thisPic < 0) {
thisPic = imgCt
}
document.Sample.src=myPix[thisPic]
}
}

So I guess I need to add support not only for the direction value but for the array number.

Any help would be appreciated.

M.
     
Grizzled Veteran
Join Date: Nov 2003
Location: Hebburn, UK
Status: Offline
Reply With Quote
Feb 23, 2005, 10:24 AM
 
Assuming you're going to be using global variables like that, then somethign like this should work:

Code:
var myPix1 = new Array("../Images/Publishing/Candy_440-475_Sample1.jpg", "../Images/Publishing/Candy_440-475_Sample2.jpg", "../Images/Publishing/Candy_440-475_Sample3.jpg", "../Images/Publishing/Candy_440-475_Sample4.jpg", "../Images/Publishing/Candy_440-475_Sample5.jpg", "../Images/Publishing/Candy_440-475_Sample6.jpg"); var myPix2 = new Array("../Images/Publishing/Candy_440-475_Sample7.jpg", "../Images/Publishing/Candy_440-475_Sample8.jpg", "../Images/Publishing/Candy_440-475_Sample9.jpg", "../Images/Publishing/Candy_440-475_Sample10.jpg", "../Images/Publishing/Candy_440-475_Sample11.jpg", "../Images/Publishing/Candy_440-475_Sample12.jpg"); var thisPic = 0; function chgSlide(direction, ar) { thisPic += direction; if (thisPic > window[ar].length-1) thisPic = 0; if (thisPic < 0) thisPic = window[ar].length-1; document.getElementById("sample").src=window[ar][thisPic]; }
Note, you'd likely want to keep separate counters for where you are in specific arrays, as at the moment, that will go to image thisPic in any array you specify.

Hope I read the question right - I've been fighting with NetBSD, so my brain's rather addled

Oh yeah: have something like this:

Code:
<img src="" id="sample" /> <button onclick="chgSlide(-1, 'myPix1')">Array one prev.</button> <button onclick="chgSlide(1, 'myPix1')">Array one next.</button> <button onclick="chgSlide(-1, 'myPix2')">Array two prev.</button> <button onclick="chgSlide(1, 'myPix2')">Array two next</button>
in your HTML.

I'll be honest - this isn't how I would go about this sort of thing if I was doing it from scratch, but in this case I thought it better to just update your code

Edit: If all the images are in the same directory, then there's no need to store that info in the array... If not, then ignore me.
(Last edited by Black Book; Feb 23, 2005 at 10:35 AM. )
Just who are Britain? What do they? Who is them? And why?

Formerly Black Book
     
Mac Elite
Join Date: May 2002
Status: Offline
Reply With Quote
Feb 23, 2005, 11:14 AM
 
Thanks Black Book.

I'm not a JS guru by any means and would be open to a simpler alternative if you have it.

I'm basically looking to have a simple script to cycle through several different galleries of images without hving to call seperate scripts to do it.

M.
     
Mac Elite
Join Date: May 2002
Status: Offline
Reply With Quote
Mar 1, 2005, 11:10 AM
 
Black Book,

If I had an id that changed for this and I wanted to feed a new id to the function each time would the id need to be declared as a variable?

<a href="javascript:chgSlide(-1, 'myPix1', 'work1');" class="slideNav">&laquo;</a>

Not sure if that question was clear?
     
Grizzled Veteran
Join Date: Nov 2003
Location: Hebburn, UK
Status: Offline
Reply With Quote
Mar 1, 2005, 12:38 PM
 
I'm assuming you mean that the image you want to change the source can be one of a number of img elements? Or something like that. Going with my initial impression, then specifying ids in the argument of the slideshow function will indeed work.

Something along the lines of:

Code:
function chgSlide(direction, ar, el) { thisPic += direction; if (thisPic > window[ar].length-1) thisPic = 0; if (thisPic < 0) thisPic = window[ar].length-1; document.getElementById(el).src=window[ar][thisPic]; }
would correspond to that.

As with all these sorts of things, a thorough understanding of what the problem is dictates the solution. So, whilst setting it up like the above is fine, it's possible that another way of doing it would be more appropriate.

Shite. Just re-read this - can you tell I've been writing technical docs all day?

Anyway, if that's nowhere near what you were asking for, then give some more details and I'll be happy to help further, or someone else can chip in. Be as verbose as ye like
Just who are Britain? What do they? Who is them? And why?

Formerly Black Book
     
Mac Elite
Join Date: May 2002
Status: Offline
Reply With Quote
Mar 1, 2005, 01:16 PM
 
Yep ... you understood it perfectly. I'm positive there's a simplier way. I'll have to post a sample at some point and get some feedback on how to simplify the code. This should work for the moment. I'm just eager to get this out of the way and move on and address the code changes later.

Thanks again for taking the time to help me ... hopefully it was a nice break from writing your technical docs ;-)
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 09:11 AM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2