Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

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

You are here: irt.org | FAQ | JavaScript | Image | Q754 [ previous next ]

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?'

©2018 Martin Webb