Changing javascript background image using an array

I am not an exprienced javascript programmer, so I try to play with javascript. I am trying to make a slideshow by clicking on a button. Function I am trying to make a function with an array that contains the names of all images and changes the background image to match the index of the array. I have only completed this part of the function and I cannot figure out 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>
      

Run codeHide result


+3


source to share


3 answers


A possible solution could be as follows:

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, here's the jsfiddle .

0


source


You are checking for the wrong variable in the state, it should be lol

, and not change

:

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

      



Also you have to process the "last image" as Nicholas suggests.

0


source


You may try:

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];
}

      

0


source







All Articles