如何每x秒显示一个新图像

How to show a new image every x seconds?

本文关键字:一个 新图像 图像 何每 显示      更新时间:2023-09-26

尝试使用JavaScript设置间隔计时器,我不确定如何将图像放在数组中。
我试了几种不同的方法,但都不行。

var myImage = document.getElementById("mainImage");
var imageArray = [img class="irc_mi ix3z7xWjrSdc-pQOPx8XEepE" alt="Image result for baseball images" style="margin-top: 70px;" src="http://totalsportscomplex.com/wp-content/uploads/2014/09/baseball-pic.jpg" onload="google.aft&&google.aft(this)" width="626" height="415",
img class="irc_mi ix3z7xWjrSdc-pQOPx8XEepE" alt="Image result for baseball images" style="margin-top: 70px;" src="http://totalsportscomplex.com/wp-content/uploads/2014/09/baseball-pic.jpg" onload="google.aft&&google.aft(this)" width="626" height="415",
img class="irc_mi ix3z7xWjrSdc-pQOPx8XEepE" alt="Image result for baseball images" style="margin-top: 70px;" src="http://totalsportscomplex.com/wp-content/uploads/2014/09/baseball-pic.jpg" onload="google.aft&&google.aft(this)" width="626" height="415"]
var imageIndex = 0;
function changeImage() {
    myImage.setAttribute("src",imageArray[imageIndex]);
  imageIndex++;
  if(imageIndex >= imageArray.length) {
  indexImage = 0;
  }
}
setInterval(changeImage,5000);

提前感谢,我也是一个新人,并试图寻找一个答案,但没有找到。

几件事

  1. 你的数组需要是一个字符串数组:

  2. 你将src属性设置为一个完整的字符串,该字符串由多于源

  3. indexImage未定义

    var myImage = document.getElementById("mainImage");
    var imageArray = [
        "http://totalsportscomplex.com/wp-content/uploads/2014/09/baseball-pic.jpg",
        "http://totalsportscomplex.com/wp-content/uploads/2014/09/baseball-pic.jpg",
        "http://totalsportscomplex.com/wp-content/uploads/2014/09/baseball-pic.jpg"
    ]
    var imageIndex = 0;
    function changeImage() {
        myImage.setAttribute("src", imageArray[imageIndex]);
        imageIndex++;
        if (imageIndex >= imageArray.length) {
            imageIndex = 0;
        }
    }
    setInterval(changeImage, 5000);
    

首先,这不是负面评论,但是,你只是不能把任何你想要的东西扔进数组。它们可以保存字符串、数字、func、obj等

  • 由于所有其他变量都是相同的,首先创建一个静态图像

<img id="mainImage"  class="irc_mi ix3z7xWjrSdc-pQOPx8XEepE" alt="Image result for baseball images" style="margin-top: 70px;" onload="google.aft&amp;&amp;google.aft(this)" width="626" height="415"/>

  • 将数组修改为

    var imageArray = [" http://totalsportscomplex.com/wp-content/uploads/2014/09/baseball-pic.jpg ", " http://totalsportscomplex.com/wp-content/uploads/2014/09/baseball-pic.jpg ", " http://totalsportscomplex.com/wp-content/uploads/2014/09/baseball-pic.jpg ")

  function changeImage() {
        myImage.setAttribute("src",imageArray[imageIndex]);
      imageIndex++;
      if(imageIndex >= imageArray.length) {
      indexImage = 0;
      }
    }

相关文章: