0

I made a div that has an image inside it and then I made a button. And I want it to be so that when I press the button it switches between multiple images that i'm going to also add.

Here is the HTML for the button and image:

#gallery {
  width: 800px;
  height: 450px;
  position: absolute;
  left: 310px;
  top: 180px;
  padding: 0;
  margin: 0;
  z-index: -1;
}

#UCL {
  position: relative;
  width: 100%;
  height: 100%;
  border-radius: 5px;
}

button {
  width: 100px;
  height: 60px;
  font-family: "Economica";
  font-size: 18px;
  background-color: #383838;
  color: white;
  font-weight: bold;
  font-size: 23px;
  border: 0;
  margin-left: 670px;
  margin-top: 110px;
  border-radius: 12px;
  position: absolute;
  opacity: 1;
}

button:hover {
  background-color: white;
  transition: ease 0.7s;
  color: black;
}
<button onclick="myFunction()"> 
  Next
</button>
<div id=gallery>
  <img id="UCL" src="http://byuinsider.com/wp-
    content/uploads/2015/09/Champions-League-Stadium-Football-Wallpapers-
    HD.jpg" alt="Stadium">
</div>
Mosh Feu
  • 28,354
  • 16
  • 88
  • 135
Musty
  • 101
  • 1
  • 10
  • where's your `myFunction()` code and the rest of your images? – Michael Coker May 21 '17 at 16:44
  • And where is the `myFunction()` code?? anyway I suggest to use jquery instead of using pure js – Mohamed-Yousef May 21 '17 at 16:45
  • oh yeah sorry, I barely have any JavaScript. I'm a beginner to it. I'll add what i have to the original post though. – Musty May 21 '17 at 16:46
  • You have **many** tutorials out there (for example: https://www.w3schools.com/howto/howto_js_slideshow.asp) Please read them and if you get stuck with a **specific** question (that you didn't find the answer, again, out there), we will glad to help you :) – Mosh Feu May 21 '17 at 16:56

2 Answers2

2

You can do it with an array containing the urls of the images, like this :

var images=['http://placeskull.com/300x300','http://placeskull.com/400x400','http://placeskull.com/500x500'];
var idx=0; // we're at the first image so, 0
function myFunction() {
  $('#UCL').attr('src',images[idx]);
  idx=(idx+1)%(images.length); // to avoid the index getting bigger than the number of images, we use the modulo operator % to make it circular, 0 1 2 0 1 2 0 1 2 ...
}
myFunction();
window.setInterval(function(){
  myFunction();
}, 2000); // 2000ms, change to whatever timing you want
#gallery {
  width: 800px;
  height: 450px;
  position: absolute;
  left: 310px;
  top: 180px;
  padding: 0;
  margin: 0;
  z-index: -1;
}

#UCL {
  position: relative;
  width: 100%;
  height: 100%;
  border-radius: 5px;
}

button {
  width: 100px;
  height: 60px;
  font-family: "Economica";
  font-size: 18px;
  background-color: #383838;
  color: white;
  font-weight: bold;
  font-size: 23px;
  border: 0;
  margin-left: 670px;
  margin-top: 110px;
  border-radius: 12px;
  position: absolute;
  opacity: 1;
}

button:hover {
  background-color: white;
  transition: ease 0.7s;
  color: black;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button onclick="myFunction()" id="nextImage"> 
  Next
</button>
<div id=gallery>
  <img id="UCL" src="" alt="Puppy">
</div>
DjaouadNM
  • 22,013
  • 4
  • 33
  • 55
1

this is one basic way of doing it and should get you started...

imgs=["images/photo1.jpg","images/photo2.jpg","images/photo3.jpg"];
currentImg=0;
function myFunction(){
    if (currentImg<imgs.length-1){
        currentImg++
    }
    else {
        currentImg=0;
    }
    document.querySelector("#UCL").src=imgs[currentImg];
}

PS suggestions to use query instead of vanilla js when still learning is nonsense.

BigBalli
  • 762
  • 1
  • 6
  • 10