简体   繁体   English

javascript背景图像更改使用数组

[英]javascript background image change using array

I am not exprienced javascript programmer so I try to play with javascript. 我不是经验丰富的javascript程序员,所以我尝试使用javascript。 I am trying to make a slideshow by clicking on a button. 我想通过点击一个按钮来制作幻灯片。 Function I am trying to make a function with array that holds the names of all the images and changing the background-image according to the index of the array. 函数我试图用数组创建一个函数,该数组包含所有图像的名称,并根据数组的索引更改背景图像。 I did only this part of function yet and I cant get what is wrong. 我只做了这部分功能而且我不知道出了什么问题。

 function change(lol){ var img = ["veni1.jpg", "veni2.jpg", "veni3"]; var middle = document.getElementById("vvvmiddle"); var index = img.indexOf(middle.style.backgroundImage); if(change === "right"){ var current = index + 1; middle.style.backgroundImage = img[current]; } } 
 middle { width:1262px; height:550px; background-color: white; margin-left: -7px; } #vvvmiddle { width:700; height:400; background-image:url('veni1.jpg'); margin: 20px 0px 0px 310px; float:left; } #sipka { width:40; height:40; border-radius: 100px; background-color: #DCDCDC; float:right; margin: 450px 410px 0px 0px; } #sipkatext { font-family: Impact; color: white; font-size: 30px; padding-left: 10px; padding-top: 1px; } #sipkaurl { text-decoration: none; } #sipka:hover { background-color: #3399FF; } #sipka2:hover { background-color: #3399FF; } #sipka2 { width:40; height:40; border-radius: 100px; background-color: #DCDCDC; float:right; margin: 450px -100px 0px 0px; } #sipkatext2 { font-family: Impact; color: white; font-size: 30px; padding-left: 13px; padding-top: 1px; } 
 <div id="middle"> <div id="vvvmiddle"> <div id="sipka" onclick="change('left')"> <div id="sipkatext"> <</div> </div> <div id="sipka2" onclick="change('right')"> <div id="sipkatext2">></div> </div> </div> </div> 

You are checking wrong variable in condition, it should be lol , not change : 您在条件中检查错误的变量,它应该是lol ,而不是change

if(lol === "right"){
    var current = index + 1;
    middle.style.backgroundImage = img[current];
}

Also you should handle "last image" case like Nicolas suggests. 你也应该像尼古拉斯建议的那样处理“最后的形象”案件。

You can try with that: 你可以尝试:

function change(lol) {
    var img = ["veni1.jpg", "veni2.jpg", "veni3"];
    var middle = document.getElementById("vvvmiddle");
    var index = img.indexOf(middle.style.backgroundImage);

    if(lol === "right"){
        index = (index + 1) % img.length;
    } else {
        index = (index + img.length - 1) % img.length;
    }
    middle.style.backgroundImage = img[index];
}

A possible solution may be this one: 一个可能的解决方案可能是这个:

var img = ["img1.png", "img2.png", "img3.png"];
var len = img.length;
var url = 'Some url...';
var current=0;

var middle = document.getElementById("vvvmiddle");
middle.style.backgroundImage = "url(" + url + img[current] + ")";

function change(dir){
    if(dir == "right" && current < len-1){
        current++;
        middle.style.backgroundImage = "url(" + url + img[current] + ")";
    } else if(dir == "left" && current > 0){
        current--;
        middle.style.backgroundImage = "url(" + url + img[current] + ")";
    }
}

See it in action, check here jsfiddle . 看到它在行动,请在这里查看jsfiddle

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

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