简体   繁体   English

Javascript多个幻灯片不起作用

[英]Javascript multiple slideshow not working

I'm trying to create several automatic slideshows on one page but it won't work. 我正在尝试在一页上创建多个自动幻灯片放映,但无法正常工作。 I found this pretty simple Javascript code online but the images won't change, instead it just stays on the first picture for all slideshows. 我在线上找到了这个非常简单的Javascript代码,但是图像不会改变,相反,它只停留在所有幻灯片的第一张照片上。 Could anyone tell me if there is something wrong with the code or if there is a better way of doing this please? 谁能告诉我代码是否有问题,或者是否有更好的方法? I'm a newbie when it comes to Javascript. 我是Java的新手。

  function swapImage(id, path)
  {
    var el = document.getElementById(id);
    el.count = el.count || 0;
    el.src = path[el.count];
      el.count = (el.count + 1) % path.length;
  }
  function slideshow() {
      setInterval(function () {
        swapImage('slide1', [
            "pix/cathedral.jpg",
            "pix/fence.jpg",
            "pix/decor.jpg",

        ]);
        swapImage('slide2', [
            "muser/front.jpg",
        "muser/window.jpg",
        "muser/decor.jpg",
        "muser/repair.jpg",

        ]);
    }, 3000);
  }
  onload = slideshow;

Then in the HTML I put this: 然后在HTML中输入:

<img id="slide1" height="500px" width="100%" src="pix/cathedral.jpg">
<img id="slide2" height="500px" width="100%" src="muser/front.jpg">

I think it almost does the same. 我认为它几乎一样。 Added custom delay time just to add a bit more flexibility 添加了自定义延迟时间,只是为了增加灵活性

function setSlider(element, sliderArray, delay) {
    var arrIndex = 0
    setInterval(function () {
        element.src = sliderArray[arrIndex]
        if (arrIndex + 1 === sliderArray.length) {
            arrIndex = 0
        } else {
            arrIndex += 1
        }
    }, delay)
}

//usage
setSlider(document.getElementById('slide1'),
    [
        "pix/cathedral.jpg",
        "pix/fence.jpg",
        "pix/decor.jpg",
    ]
    , 1000)

setSlider(document.getElementById('slide2'),
    [
        "muser/front.jpg",
        "muser/window.jpg",
        "muser/decor.jpg",
        "muser/repair.jpg",
    ]
    , 1000)

The first problem is the count property you are referencing in the code; 第一个问题是您在代码中引用的count属性。 the count property of a DOM element does not exist and nowhere in the code do you create it. DOM元素的count属性不存在,并且您在代码中的任何地方都不会创建它。 To read more about the DOM and what properties it contains visit here https://www.w3schools.com/jsref/dom_obj_all.asp . 要了解有关DOM及其包含的属性的更多信息,请访问https://www.w3schools.com/jsref/dom_obj_all.asp JavaScript is a difficult language for even veteran programmers to debug, because if something is wrong it just stops working, you then have to use the console in developer tools or a CLI debug program to step through your code. JavaScript是即使资深的程序员也难以调试的语言,因为如果出了什么问题,JavaScript就会停止工作,那么您就必须使用开发人员工具中的控制台或CLI调试程序来逐步执行代码。 Eclipse has a good javascript plugin. Eclipse有一个不错的javascript插件。 See more about Eclipse here http://www.eclipse.org/ . http://www.eclipse.org/上了解有关Eclipse的更多信息。 If you plan to spend time doing JavaScript programming with HTML it pays to throughly familiarize yourself with the DOM. 如果您打算花时间用HTML进行JavaScript编程,则需要完全熟悉DOM。 When using the DOM the only things that are available to work with nodes, is the interface that is provided through the DOM specification. 使用DOM时,可用于节点的唯一事物就是通过DOM规范提供的接口。 Because your el node that is retrieved in the first line of code in the the first function declaration is a DOM node the only tools you have to work with are the interface tools provided. 因为在第一个函数声明的第一行代码中检索到的el节点是DOM节点,所以唯一需要使用的工具就是所提供的接口工具。 Which means you can't add things the same way you would in standard JavaScript. 这意味着您不能像在标准JavaScript中那样添加内容。 In JavaScript it is perfectly acceptable to take something like el.count = 0; 在JavaScript中,完全可以接受el.count = 0; now el has the property count with a value of 0. In the DOM that is not acceptable. 现在el的属性count的值为0。在DOM中是不可接受的。 You have to use the interface provided in the above mentioned link. 您必须使用上述链接中提供的界面。 You have to remember that the DOM is not a JavaScript object, it is a document object that provides an interface to work with JavaScript via the DOM. 您必须记住,DOM不是JavaScript对象,而是一个文档对象,它提供了通过DOM与JavaScript一起使用的接口。

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

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