简体   繁体   English

(Javascript)幻灯片不工作

[英](Javascript) SlideShow Not Working

Hello fellow stackoverflow members, I've been trying to make a Slideshow.你好,stackoverflow 成员,我一直在尝试制作幻灯片。 I've referenced from many other sites including this one but the pictures aren't showing up in the container element nor are the "prev" and "next" buttons functioning properly.我参考了许多其他网站,包括这个网站,但图片没有显示在容器元素中,“上一个”和“下一个”按钮也没有正常工作。 I'd appreciate it if I got help!如果我得到帮助,我将不胜感激! :) :)

my code:我的代码:

var photos = newArray ();
    photos[0] = "img/image(2).jpg";
    photos[1] =  "img/image(4).jpg";
    photos[2] = "img/image(6).jpg";
    photos[3] = "img/image(8).jpg";
    photos[4] = "img/image(10).jpg";
    photos[5] = "img/image(12).jpg";
    photos[6] = "img/image(14).jpg";
    photos[7] = "img/image(16).jpg";
    photos[8] = "img/image(18).jpg";
    photos[9] = "img/image(20).jpg";
    photos[10] = "img/image(22).jpg";
    photos[11] = "img/image(24).jpg"
    //END OF PHOTOS ARRAY//
var i = 0;
var k = photos.length-1;
function next.onclick() {
var img= document.getElementById("image-container");
img.src = photos[i];
if (i < k ) {
    i++;
}else {
i = 0; }

} 
function prev.onclick() {
var img= document.getElementById("image-container");
img.src=photos[i];
if)i > 0) {i--;}
else {i = k; }
}
getImageArray = function(containerId) {
    var containerElement = document.getElementById(container);
    if (containerElement) {
        var imageArray = containerElement.getElementById("container");
        return photos[i];
    } else {
        return null;
    }
}

this is what my slideshow looks like (it's broken) http://prntscr.com/5dcfzq The share button isn't important, I can make that work at least.这就是我的幻灯片的样子(坏了) http://prntscr.com/5dcfzq分享按钮并不重要,我至少可以让它发挥作用。 The main problem is that the pictures aren't showing and the back and foward buttons are messed up :'(主要问题是图片没有显示,后退和前进按钮搞砸了:'(

ps ( I'm not sure if part of the reason is how I'm linking to the "next" or "back" functions with the div tag, because i'm this is how i'm doing it : ps(我不确定部分原因是否是我如何使用 div 标签链接到“next”或“back”函数,因为我就是这样做的:

<div id = "back" onclick = "prev()"></div>

OK ... to summarize ...好的……总结一下……

1. var photos = newArray (); 1. var photos = newArray ();

  • There needs to be a space between new and Array, so ... new 和 Array 之间需要有一个空格,所以...

    var photos = new Array(); var 照片 = new Array();

2. function prev.onclick() { needs to be just function prev() { 2. function prev.onclick() {需要只是function prev() {

3. Same with next.onclick() based on usage in HTML. 3.next.onclick()相同,基于 HTML 中的用法。

4. In prev() ... if)i > 0) {i--;} should be ... 4.prev() ... if)i > 0) {i--;}应该是 ...

if (i > 0) { i--; }

5. WRONG: Also in prev()' ... else should be i = k-1;` 5.错误:同样在prev()' ... else should be i = k-1;`

6. DO NOT NEED Not sure why you have the getImageArray function at all. 6.不需要不知道为什么你有 getImageArray 函数。

7. This assumes there is an '' tag in the HTML. 7.假设 HTML 中有一个 '' 标签。

UPDATE :更新

Here's the code that works ... this all goes in the body:这是有效的代码......这一切都在正文中:

These are my assumptions in the body ...这些是我对身体的假设...

<img id="image-container" />
<div id="back" onclick="prev()">Previous</div>
<div id="next" onclick="mext()">Next</div>

The script code MUST be at the end of the body ...脚本代码必须在正文的末尾...

<script>
    var photos = new Array ();
        photos[0] = "img/image(2).jpg";
        photos[1] =  "img/image(4).jpg";
        photos[2] = "img/image(6).jpg";
        photos[3] = "img/image(8).jpg";
        photos[4] = "img/image(10).jpg";
        photos[5] = "img/image(12).jpg";
        photos[6] = "img/image(14).jpg";
        photos[7] = "img/image(16).jpg";
        photos[8] = "img/image(18).jpg";
        photos[9] = "img/image(20).jpg";
        photos[10] = "img/image(22).jpg";
        photos[11] = "img/image(24).jpg"
        //END OF PHOTOS ARRAY//

    // Here, I set the img variable so that it can be re-used.
    // I also loaded the first image ...
    var i = 0;
    var k = photos.length-1;
    var img = document.getElementById("image-container");
    img.src = photos[i];

    function next() {
        img.src = photos[i];
        if (i<k) {
            i++;
        } else {
            i = 0;
        }
    } 
    function prev() {
        img.src=photos[i];
        if (i>0) {
            i--;
        } else {
            i = k;
        }
    }
</script>

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

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