Change image in HTML page every few seconds

前端 未结 6 1192
死守一世寂寞
死守一世寂寞 2020-11-28 06:57

I want to change images every few seconds, this is my code:




        
相关标签:
6条回答
  • 2020-11-28 07:08

    Best way to swap images with javascript with left vertical clickable thumbnails

    SCRIPT FILE: function swapImages() {

        window.onload = function () {
            var img = document.getElementById("img_wrap");
            var imgall = img.getElementsByTagName("img");
            var firstimg = imgall[0]; //first image
            for (var a = 0; a <= imgall.length; a++) {
                setInterval(function () {
                    var rand = Math.floor(Math.random() * imgall.length);
                    firstimg.src = imgall[rand].src;
                }, 3000);
    
    
    
                imgall[1].onmouseover = function () {
                     //alert("what");
                    clearInterval();
                    firstimg.src = imgall[1].src;
    
    
                }
                imgall[2].onmouseover = function () {
                    clearInterval();
                    firstimg.src = imgall[2].src;
                }
                imgall[3].onmouseover = function () {
                    clearInterval();
                    firstimg.src = imgall[3].src;
                }
                imgall[4].onmouseover = function () {
                    clearInterval();
                    firstimg.src = imgall[4].src;
                }
                imgall[5].onmouseover = function () {
                    clearInterval();
                    firstimg.src = imgall[5].src;
                }
            }
    
        }
    
    
    }
    
    0 讨论(0)
  • 2020-11-28 07:13

    As of current edited version of the post, you call setInterval at each change's end, adding a new "changer" with each new iterration. That means after first run, there's one of them ticking in memory, after 100 runs, 100 different changers change image 100 times every second, completely destroying performance and producing confusing results.

    You only need to "prime" setInterval once. Remove it from function and place it inside onload instead of direct function call.

    0 讨论(0)
  • 2020-11-28 07:18

    You can load the images at the beginning and change the css attributes to show every image.

    var images = array();
    for( url in your_urls_array ){
       var img = document.createElement( "img" );
       //here the image attributes ( width, height, position, etc )
       images.push( img );
    }
    
    function player( position )
    {
      images[position-1].style.display = "none" //be careful working with the first position
      images[position].style.display = "block";
      //reset position if needed
      timer = setTimeOut( "player( position )", time );
    }
    
    0 讨论(0)
  • 2020-11-28 07:21

    As I posted in the comment you don't need to use both setTimeout() and setInterval(), moreover you have a syntax error too (the one extra }). Correct your code like this:

    (edited to add two functions to force the next/previous image to be shown)

    <!DOCTYPE html>
    
    <html>
       <head>
          <title>change picture</title>
          <script type = "text/javascript">
              function displayNextImage() {
                  x = (x === images.length - 1) ? 0 : x + 1;
                  document.getElementById("img").src = images[x];
              }
    
              function displayPreviousImage() {
                  x = (x <= 0) ? images.length - 1 : x - 1;
                  document.getElementById("img").src = images[x];
              }
    
              function startTimer() {
                  setInterval(displayNextImage, 3000);
              }
    
              var images = [], x = -1;
              images[0] = "image1.jpg";
              images[1] = "image2.jpg";
              images[2] = "image3.jpg";
          </script>
       </head>
    
       <body onload = "startTimer()">
           <img id="img" src="startpicture.jpg"/>
           <button type="button" onclick="displayPreviousImage()">Previous</button>
           <button type="button" onclick="displayNextImage()">Next</button>
       </body>
    </html>
    
    0 讨论(0)
  • 2020-11-28 07:23

    Change setTimeout("changeImage()", 30000); to setInterval("changeImage()", 30000); and remove var timerid = setInterval(changeImage, 30000);.

    0 讨论(0)
  • 2020-11-28 07:25

    below will change link and banner every 10 seconds

       <script>
            var links = ["http://www.abc.com","http://www.def.com","http://www.ghi.com"];
            var images = ["http://www.abc.com/1.gif","http://www.def.com/2.gif","http://www.ghi.com/3gif"];
            var i = 0;
            var renew = setInterval(function(){
                if(links.length == i){
                    i = 0;
                }
                else {
                document.getElementById("bannerImage").src = images[i]; 
                document.getElementById("bannerLink").href = links[i]; 
                i++;
    
            }
            },10000);
            </script>
    
    
    
    <a id="bannerLink" href="http://www.abc.com" onclick="void window.open(this.href); return false;">
    <img id="bannerImage" src="http://www.abc.com/1.gif" width="694" height="83" alt="some text">
    </a>
    
    0 讨论(0)
提交回复
热议问题