0

hey guys, so im trying to do the image slider with js but its not working. only the first pic: pic1, displays then it doesnt slide.

code:

<script type="text/javascript">
        <!-->
        var img1=new Image()
        img1.src="image/pic1.jpg"
        var img2=new Image()
        img2.src="image/pic2.jpg"
        var img3=new Image()
        img3.src="image/pic4.jpg"
        var img4=new Image()
        img4.src="image/pic5.jpg"
        var img5=new Image()
        img5.src="image/pic3.jpg"
        var img6=new Image()
        img6.src="image/pic6.jpg"
        var img7=new Image()
        img7.src="image/pic7.jpg"
        var img8=new Image()
        img8.src="image/pic8.jpg"
        var img9=new Image()
        img9.src="image/pic9.jpg"
        //-->
    </script>
    <div id="slideshow" align="left" style="margin-left: 30px;"><!--slideshow-->
        <img src="image/pic1.jpg" name="slide" width="450" height="200"/>
        <script type="text/javascript"><!--javascript for slideshow-->
        <!--
        var pic=1
        function slideit()
        {
            document.image.slide.src=eval("img"+pic+".src")
            if(pic<9)
            pic++
            else 
            pic=1
            setTimeout("slideit()",2000)
        }
        slideit()
        //-->
        <!--end javascript for slideshow-->
        </script>
    </div>

thanks in advance for any help!

3
Contributors
4
Replies
15
Views
3 Years
Discussion Span
Last Post by nadiam
0

Made some adjustments to your code...

<!DOCTYPE html>
<html>
<head>

</head>
<body>

<script>

        var img1=new Image()
        img1.src="image/pic1.jpg"
        var img2=new Image()
        img2.src="image/pic2.jpg"
        var img3=new Image()
        img3.src="image/pic4.jpg"
        var img4=new Image()
        img4.src="image/pic5.jpg"
        var img5=new Image()
        img5.src="image/pic3.jpg"
        var img6=new Image()
        img6.src="image/pic6.jpg"
        var img7=new Image()
        img7.src="image/pic7.jpg"
        var img8=new Image()
        img8.src="image/pic8.jpg"
        var img9=new Image()
        img9.src="image/pic9.jpg"
        //-->
</script>
    <div id="slideshow" align="left" style="margin-left: 30px;"><!--slideshow-->
        <img src="image/pic1.jpg" id="slide" width="450" height="200"/>
        <script type="text/javascript"><!--javascript for slideshow-->
        <!--
        var pic=1
        function slideit()
        {
            //document.image.slide.src=eval("img"+pic+".src")
        document.getElementById("slide").src = "image/pic" + pic + ".jpg";
            if(pic<9) {
            pic++;
            } else { 
            pic=1;
        }
            setTimeout("slideit()",2000);
        }
        slideit()
        //-->
        <!--end javascript for slideshow-->
        </script>
    </div>

</body>
</html>

Edited by JorgeM

0

Shorter version of your code...

<!DOCTYPE html>
<html>
<head>

</head>
<body>

<div id="slideshow" align="left" style="margin-left: 30px;">
  <img src="image/pic1.jpg" id="slide" width="450" height="200"/>
</div>

<script>
  var imgArray = new Array();
  for (var i = 1; i <= 9;i++) {
     imgArray[i] = new Image();
     imgArray[i].src == "image/pic" + i + ".jpg";
  }

  var pic=1
  function slideit() {
     document.getElementById("slide").src = "image/pic" + pic + ".jpg";
     if (pic < 9) {
        pic++;
     } else { 
        pic=1;
     }
     setTimeout("slideit()",2000);
   }
   slideit();
</script>

</body>
</html>
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.