Musty
Musty

Reputation: 101

How can I make a button change images?

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>

Upvotes: 0

Views: 75

Answers (2)

Djaouad
Djaouad

Reputation: 22776

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>

Upvotes: 2

BigBalli
BigBalli

Reputation: 817

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.

Upvotes: 1

Related Questions