Skip to content

Instantly share code, notes, and snippets.

@sdoro
Created January 14, 2018 15:35
Show Gist options
  • Save sdoro/a5f9f15c73033fc7b11579c7d5f92697 to your computer and use it in GitHub Desktop.
Save sdoro/a5f9f15c73033fc7b11579c7d5f92697 to your computer and use it in GitHub Desktop.
images
<button id="prev"><</button>
<img id="image" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/1259621/city.jpg">
<button id="next">></button>
var prev = document.getElementById("prev");
var next = document.getElementById("next");
var image = document.getElementById("image");
// store image paths in an array
var images = ["https://s3-us-west-2.amazonaws.com/s.cdpn.io/1259621/city.jpg", "https://s3-us-west-2.amazonaws.com/s.cdpn.io/1259621/cloudy.jpg", "https://s3-us-west-2.amazonaws.com/s.cdpn.io/1259621/green.jpg", "https://s3-us-west-2.amazonaws.com/s.cdpn.io/1259621/pretty_clouds.jpg"];
var imageIndex = 0;
// point to previous image when prev button is clicked
prev.onclick = function(){
// set image to highest index when moving past 0
if(imageIndex == 0){
imageIndex = images.length - 1;
}
else{
imageIndex--;
}
image.setAttribute("src", images[imageIndex]);
}
// point to previous image when prev button is clicked
next.onclick = function(){
// set image index to 0 when moving past max index
if(imageIndex == images.length - 1){
imageIndex = 0;
}
else{
imageIndex++;
}
image.setAttribute("src", images[imageIndex]);
}
button{
width: 378;
height: 378;
background: blue;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment