Skip to content

Instantly share code, notes, and snippets.

@sdoro
Created January 14, 2018 15:35
Show Gist options
  • Select an option

  • Save sdoro/a5f9f15c73033fc7b11579c7d5f92697 to your computer and use it in GitHub Desktop.

Select an option

Save sdoro/a5f9f15c73033fc7b11579c7d5f92697 to your computer and use it in GitHub Desktop.

Revisions

  1. sdoro created this gist Jan 14, 2018.
    7 changes: 7 additions & 0 deletions images.markdown
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    images
    ------


    A [Pen](https://codepen.io/bdeen/pen/4f308cb6d1bffb1b46cb75bdbd93896e) by [Bre'Ana Deen](https://codepen.io/bdeen) on [CodePen](https://codepen.io).

    [License](https://codepen.io/bdeen/pen/4f308cb6d1bffb1b46cb75bdbd93896e/license).
    3 changes: 3 additions & 0 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    <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>
    30 changes: 30 additions & 0 deletions script.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    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]);
    }
    5 changes: 5 additions & 0 deletions style.css
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    button{
    width: 378;
    height: 378;
    background: blue;
    }