Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript Navigate images with javascript

Crack

New Coder
HTML:
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link
      rel="stylesheet"
      href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css"
    />
    <link rel="stylesheet" href="style.css" />
    <title>Carousel</title>
  </head>
  <body>
    <div class="container">
      <div class="image fade">
        <img src="./img/Lam1.jpeg" alt="Lamborghini Image" width="100%" />
      </div>
      <div class="image fade">
        <img src="./img/Lam2.jpeg" alt="Lamborghini Image" width="100%" />
      </div>
      <div class="image fade">
        <img src="./img/Lam3.jpeg" alt="Lamborghini Image" width="100%" />
      </div>
      <div class="image fade">
        <img src="./img/Lam4.jpeg" alt="Lamborghini Image" width="100%" />
      </div>
      <div class="image fade">
        <img src="./img/Lam5.jpeg" alt="Lamborghini Image" width="100%" />
      </div>
      <div class="image fade">
        <img src="./img/Lam6.webp" alt="Lamborghini Image" width="100%" />
      </div>
      <div class="image fade">
        <img src="./img/Lam7.webp" alt="Lamborghini Image" width="100%" />
      </div>
      <div class="image fade">
        <img src="./img/Lam8.jpeg" alt="Lamborghini Image" width="100%" />
      </div>
      <i class="fa fa-chevron-left prev" aria-hidden="true"></i>
      <i class="fa fa-chevron-right next" aria-hidden="true"></i>

      <div class="dots">
        <span class="dot"></span>
        <span class="dot"></span>
        <span class="dot"></span>
        <span class="dot"></span>
        <span class="dot"></span>
        <span class="dot"></span>
        <span class="dot"></span>
        <span class="dot"></span>
      </div>
    </div>
  </body>
</html>

JavaScript:
// Selectors
const images = document.querySelectorAll(".image");
const previous = document.querySelector(".prev");
const next = document.querySelector(".next");
const dots = document.querySelectorAll(".dot");

// Event Listeners
previous.addEventListener("click", showPrevImg);
next.addEventListener("click", showNextImg);

//Functions
let index = 0;

showImg();

function showImg() {
  for (let i = 0; i < images.length; i++) {
    if (i) {
      images[i].style.display = "block";
      dots[i].classList.add("active");
    } else {
      images[i].style.display = "none";
      dots[i].classList.remove("active");
    }
    console.log(images[i]);
  }
}
function showPrevImg() {
  index--;

  if (index < 0) {
    index = images.length - 1;
  }
  showImg();
}

function showNextImg() {
  index++;
  if (index > images.length - 1) {
    index = 0;
  }
  showImg();
}
console.log(showImg());
CSS:
.image {
  display: none;
}

I am trying show my images through the javascript code after a display of none in css and also use previous and next arrows to skip through the images but my images are not showing to begin with. Any help is appreciated.
 
HTML:
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" />
    <base href="https://codeforum.org/styles/default/xenforo/smilies/emojione/">
    <style>
    .image{
        display: none;
    }
    .active{
        color: crimson;
        font-weight: bold;
    }
    .vis{
        display: block;
    }
    [aria-hidden="true"]{
        cursor: pointer;
    }
    .dot{
        font-size: 48px;
    }
    </style>
    <title>Carousel</title>
  </head>
  <body>
    <div class="container">
      <div class="image fade">
        <img src="x3.png" alt="Lamborghini Image" width="50" />
      </div>
      <div class="image fade">
        <img src="ninja.png" alt="Lamborghini Image" width="50" />
      </div>
      <div class="image fade">
        <img src="alien.png" alt="Lamborghini Image" width="50" />
      </div>
      <div class="image fade">
        <img src="coffee.png" alt="Lamborghini Image" width="50" />
      </div>
      <div class="image fade">
        <img src="thumbsup.png" alt="Lamborghini Image" width="50" />
      </div>
      <div class="image fade">
        <img src="inlove.png" alt="Lamborghini Image" width="50" />
      </div>
      <div class="image fade">
        <img src="thumbsdown.png" alt="Lamborghini Image" width="50" />
      </div>
      <div class="image fade">
        <img src="laugh.png" alt="Lamborghini Image" width="50" />
      </div>
      <i class="fa fa-chevron-left prev" aria-hidden="true"></i>
      <i class="fa fa-chevron-right next" aria-hidden="true"></i>

      <div class="dots">
        <span class="dot">.</span>
        <span class="dot">.</span>
        <span class="dot">.</span>
        <span class="dot">.</span>
        <span class="dot">.</span>
        <span class="dot">.</span>
        <span class="dot">.</span>
        <span class="dot">.</span>
      </div>
    </div>
    <script>
    // Selectors
const images = document.querySelectorAll(".image");
const previous = document.querySelector(".prev");
const next = document.querySelector(".next");
const dots = document.querySelectorAll(".dot");

// Event Listeners
previous.addEventListener("click", showPrevImg);
next.addEventListener("click", showNextImg);

//Functions
let index = 0;

showImg();

function showImg() {
  document.querySelector('.vis')?.classList.remove('vis');
  document.querySelector('.dot.active')?.classList.remove('active');
  images[index].classList.add('vis');
  dots[index].classList.add('active');
}
function showPrevImg() {
  index--;

  if (index < 0) {
    index = images.length - 1;
  }
  showImg();
}

function showNextImg() {
  index++;
  if (index > images.length - 1) {
    index = 0;
  }
  showImg();
}

    </script>
  </body>
</html>
 

Buy us a coffee!

Back
Top Bottom