A Pen by Bre'Ana Deen on CodePen.
Created
January 14, 2018 15:35
-
-
Save sdoro/a5f9f15c73033fc7b11579c7d5f92697 to your computer and use it in GitHub Desktop.
images
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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