Home Articles FAQs XREF Games Software Instant Books About Feedback Search Site-Map
irt.org logo

Q754 How can I create a functions that cycle foward and backward through an array of images?

irt.org | Knowledge Base | JavaScript | Image | Q754 [ previous next ]

Q754 How can I create a functions that cycle foward and backward through an array of images?

It depends what you mean by an array of images. If you mean something like:

<SCRIPT LANGUAGE="JavaScript"><!--
var arrayOfImages = new Object();

var index = 0;

arrayOfImages[index++] = 'picture1.gif';
arrayOfImages[index++] = 'picture2.gif';
arrayOfImages[index++] = 'picture3.gif';
arrayOfImages[index++] = 'picture4.gif';
arrayOfImages[index++] = 'picture5.gif';
//--></SCRIPT>

Then you can use:

<SCRIPT LANGUAGE="JavaScript"><!--
var start = 0;

function next() {
    if (document.images) {
        start++;
        if (start > index)
            start = index;
        else
            document.images['imageName'].src = arrayOfImages[start];
    }
}

function last() {
    if (document.images) {
        start--;
        if (start < 0)
            start = index;
        else
            document.images['imageName'].src = arrayOfImages[start]; 
    }
}
//--></SCRIPT>

Feedback on 'Q754 How can I create a functions that cycle foward and backward through an array of images?'


Provide feedback ...
AddThis Social Bookmark Button

Provide feedback ... AddThis Social Bookmark Button


Last Updated: 30th March 2008. Maintained by: Martin Webb and Michel Plungjan
irt.org liability, trademark, document use, privacy statement and software licensing rules apply.
Copyright © 1996-2008 irt.org, All Rights Reserved.