Image Slideshow

Project Description

A slideshow with an array of images from Dublin city, Ireland.

You can view a completed version of the project here.

Your project folder

In your portfolio/js folder is a folder named image-slideshow that contains all the files you need for this JavaScript project.

In VS Code, open the project’s index.html web page and ensure the hyperlinks and personal details have been correctly updated.

Adding the JavaScript code

Follow the steps below to add the JavaScript code to the index.html file.

  1. Scroll down the web page until you see an empty pair of opening <script> and closing </script> tags.
  2. Click inside the empty pair of tags and paste the JavaScript code below.
    (function() {
       const pictures = [
          "dublin-0",
          "dublin-1",
          "dublin-2",
          "dublin-3",
          "dublin-4",
          "dublin-5"
    ];
    
    // Select both left and right button. Add event listeners
    const buttons = document.querySelectorAll('.btn')
    const imageDIV = document.querySelector('.container-img')
    let counter = 0
    
    buttons.forEach(function(button){
       button.addEventListener('click', function(e) {
    
          if (button.classList.contains('btn-left')) {
             counter--;
             if (counter < 0) {
                counter = pictures.length - 1;
             }
             imageDIV.style.backgroundImage = `url('assets/img/${pictures[counter]}.jpg')`
             console.log(imageDIV.style.backgroundImage = `url('assets/img/${pictures[counter]}.jpg')`)
          }
    
          if (button.classList.contains('btn-right')) {
             counter++;
             if (counter > pictures.length - 1) {
             counter = 0;
          }
          imageDIV.style.backgroundImage = `url('assets/img/${pictures[counter]}.jpg')`
          console.log(imageDIV.style.backgroundImage = `url('assets/img/${pictures[counter]}.jpg')`)
          }
        }) // Ends loop for left and right arrows
       })
    })();
  3. Save your index.html file.
  4. Display the file in a web browser and verify it works correctly.

Uploading your project to GitHub

Upload the images-slideshow folder as a sub-folder of the portfolio/js folder on your GitHub account.