Image Scrolling Problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jason Ferguson

    #1

    Image Scrolling Problem

    I have created some simple code to allow me to flip through thumbnail
    images via "Previous" and "Next" links. The idea is that I can have
    multiple sets of images that can be scrolled through, via
    document.images[<n>].src.

    However, the problem is, once the "last" image is reached, the code
    refuses to go back to the "first" image.

    Help is appreciated.

    <title>Test Page</title>
    </head>

    <script language="javas cript">
    <!--
    var imagearray1 = new Array("./image1.gif","./image2.gif");
    var lastelement1 = imagearray1.len gth - 1;
    var currentelement1 = 0;


    function showpicture(ima geindex, imagearray, current)
    {
    document.images[imageindex].src=imagearray[current];
    }

    function nextpicture(ima geindex, imagearray, current, last)
    {
    current += 1;

    if (current>last)
    {
    current = 0;
    }

    showpicture(ima geindex, imagearray, current);


    }

    function prevpicture(ima geindex, imagearray, current, last)
    {

    current -= 1;

    if (current < 0)
    {
    current = last;
    }

    showpicture(ima geindex, imagearray, current);

    }


    -->
    </script>

    <body>
    <img src="image1.gif "><br>
    <a href="javascrip t:prevpicture(0 , imagearray1, currentelement1 ,
    lastelement1)"> Previous</a><br>
    <a href="javascrip t:nextpicture(0 , imagearray1, currentelement1 ,
    lastelement1)"> Next</a><br>

    </body>
  • Michael Winter

    #2
    Re: Image Scrolling Problem

    On 29 Jan 2004 07:09:31 -0800, Jason Ferguson
    <fergusonjason@ sbcglobal.net> wrote:
    [color=blue]
    > I have created some simple code to allow me to flip through thumbnail
    > images via "Previous" and "Next" links. The idea is that I can have
    > multiple sets of images that can be scrolled through, via
    > document.images[<n>].src.
    >
    > However, the problem is, once the "last" image is reached, the code
    > refuses to go back to the "first" image.[/color]

    If you tried to test the script as it is with more than two images, you
    would find bigger issues than not being able to wrap between the first and
    last images.

    Declaration:
    var currentelement1 = 0;

    Definition:
    function nextpicture(ima geindex, imagearray, current, last) {
    current += 1;
    if (current>last) {
    current = 0;
    }
    showpicture(ima geindex, imagearray, current);
    }

    Call:
    nextpicture(0, imagearray1, currentelement1 , lastelement1)

    When called, nextpicture is passed 0 for the third argument (current in
    the function). This value is modified in the function, but currentelement1
    will still remain 0; the variable is not passed by reference. The simple
    solution here is to use the global variable directly.

    As for the non-functional wrapping, you're forgetting how arrays are
    indexed: 0 to n-1. You are treating them as 0 to n. The fix here is:

    if( current == last ) current = 0;

    and

    if( 0 > current ) current = last - 1;

    for nextpicture and prevpicture, respectively.
    [color=blue]
    > <a href="javascrip t:prevpicture(0 , imagearray1, currentelement1 ,
    > lastelement1)"> Previous</a><br>
    > <a href="javascrip t:nextpicture(0 , imagearray1, currentelement1 ,
    > lastelement1)"> Next</a>[/color]

    Finally, change the above links to use the onclick attribute instead of
    the JavaScript pseudo-protocol in the href attribute. This doesn't have
    any direct bearing on the problem, but it is a bad habit (for the most
    part), and should be avoided. It is covered in the group FAQ:



    Mike

    --
    Michael Winter
    M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

    Comment

    Working...