简体   繁体   English

图像轮播无法升级(HTML / JavaScript)

[英]Carousel of Images Will Not Advance (HTML/JavaScript)

I am attempting to have the carousel start immediately upon visiting the page and cycle through a series of images every 2.5 seconds, however it will not advance past the first image. 我正在尝试让轮播在访问页面后立即启动,并每隔2.5秒循环显示一系列图像,但是它不会前进到第一幅图像。 I've looked around here and w3schools but haven't been able to locate where my issue is. 我到过这里和w3schools,但是找不到我的问题所在。 Any help would be greatly appreciated! 任何帮助将不胜感激!

JavaScript is as follows: JavaScript如下:

window.onload slideShow();
var i=0;

function slideShow()    {
    window.setInterval("nextSlide()", 2500);
}

function nextSlide()    {
    var images["images/stockton0.jpg",
        "images/stockton1.jpg"
        "images/stockton2.jpg"
        "images/stockton3.jpg"
        "images/stockton4.jpg"
        "images/stockton5.jpg"
        "images/stockton6.jpg"
        "images/stockton7.jpg"
        "images/stockton8.jpg"
        "images/stockton9.jpg"
        "images/stockton10.jpg"]
    var photo = document.getElementByClass("stocktonPics");
    photo.src = images[i];
    i++;
}

HTML code: HTML代码:

<img class="stocktonPics" src="images/stockton0.jpg" alt="slides">

A good tip is to check the console for errors. 一个很好的技巧是检查控制台是否有错误。

There's nothing wrong with the flow of your code, besides some tips on making it more maintainable, readable, or semantically correct. 除了一些使代码更具可维护性,可读性或语义正确性的提示外,代码流程没有任何问题。

You simply forgot an = in window.onload = slideShow; 您只是在window.onload = slideShow;忘记了= window.onload = slideShow;

And document.getElementByClass doesn't exist. 而且document.getElementByClass不存在。 You need to use document.getElementsByClassName(...) to get an array of elements with that class , and finally get its first item with [0] like so: 您需要使用document.getElementsByClassName(...)来获取class的元素数组,并最终使用[0]获取其第一项,如下所示:

var photo = document.getElementsByClassName("stocktonPics")[0];

Note that slideShow no longer has the () to call it, when window.onload is assigned to it. 请注意,当将window.onload分配给slideShow时,它不再具有()来调用它。 This is because you're assigning window.onload to the slideShow function, not the result of calling slideShow() , which in this case is undefined , as nothing is returned. 这是因为您正在将window.onload分配给slideShow函数,而不是调用slideShow()的结果,在这种情况下,该函数是undefined ,因为未返回任何内容。

Your image array should be assigned in this way: var images = [ a, b, c ] 您的图像数组应采用以下方式分配: var images = [ a, b, c ]

The other thing you should do is keep the array of images outside the scope of the function, so you can use it more easily and change it, rather than creating one (if you don't count optimizations) ever time the function is called. 您应该做的另一件事是将图像数组保持在函数范围之外,这样您就可以更轻松地使用它并对其进行更改,而不是每次调用该函数时都创建一个(如果不算优化的话)。 And lastly, window.setInterval( a, b ) can take either a string that will be eval() ed (or equivalent to it), which is what you did, or a function itself. 最后, window.setInterval( a, b )可以采用您所做的将是eval() ed(或等效于它)的字符串,也可以采用函数本身。 In your case, what you want is simply the function: window.setInterval( nextSlide, 2500 ) . 在您的情况下,您只需要简单的函数: window.setInterval( nextSlide, 2500 )

Here's the final full code: 这是最终的完整代码:

var i=0;
var images=[
      "images/stockton0.jpg",
      "images/stockton1.jpg",
      "images/stockton2.jpg",
      "images/stockton3.jpg",
      "images/stockton4.jpg",
      "images/stockton5.jpg",
      "images/stockton6.jpg",
      "images/stockton7.jpg",
      "images/stockton8.jpg",
      "images/stockton9.jpg",
      "images/stockton10.jpg" ];

function slideShow() {
    window.setInterval( nextSlide, 2500);
}

function nextSlide() {
    var photo = document.getElementsByClassName("stocktonPics")[0];
    photo.src = images[i];
    i++;
}

window.onload = slideShow;

getElementsByClass method is will get a like array elements object, getElementsByClass方法将获取一个类似数组元素的对象,

so, you should use [0] to get the first element object and set src attr. 因此,您应该使用[0]获取第一个元素对象并设置src attr。

var pics = document.getElementsByClassName("stocktonPics")

It's a like array elements object 这就像一个数组元素对象

var firstPic = document.getElementsByClassName("stocktonPics")[0]

It's a element object 这是一个元素对象

 let i = 0; const images = ["images/stockton0.jpg", "images/stockton1.jpg", "images/stockton2.jpg", "images/stockton3.jpg", "images/stockton4.jpg", "images/stockton5.jpg", "images/stockton6.jpg", "images/stockton7.jpg", "images/stockton8.jpg", "images/stockton9.jpg", "images/stockton10.jpg"]; const stockton = document.getElementsByClassName("example")[0]; const slideShow = () => { window.setInterval(nextSlide, 2500); } const nextSlide = () => { stockton.src = images[i]; if(i >= images.length - 1) { i = 0; } else { i++; } console.log(i, stockton) } slideShow() 
 <img class="example"> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM