Michael Philibin
Michael Philibin

Reputation: 403

Javascript image slideshow not selecting the initial image upon page load

I'll preface by saying I am a javascript novice.

The Issue: I have a 5 image slideshow using javascript that, upon loading the webpage, displays the images below one another going down the page rather than overlapping one another.

As soon as I select the "left" or "right" toggle arrow to move to the next image, the images then overlap one another correctly and the correct image is displayed. Until I refresh the page, the slideshow works properly.

The HTML, CSS, and Java is as follows:

HTML:

<div class="image_slider">
    <div class="slideshow-container">
      <div class="mySlides fade">
          <img src="slider_3.png" style="width:100%">
      </div>

      <div class="mySlides fade">
          <img src="slider_2.png" style="width:100%">
      </div>

      <div class="mySlides fade">
        <img src="slider_1.png" style="width:100%">
      </div>

      <div class="mySlides fade">
        <img src="slider_4.png" style="width:100%">
      </div>

      <div class="mySlides fade">
        <img src="slider_5.png" style="width:100%">
      </div>

      <a class="prev" onclick="plusSlides(-1)">&#10094;</a>
      <a class="next" onclick="plusSlides(1)">&#10095;</a>
    </div>

    <br>

    <div style="text-align:center">
      <span class="dot" onclick="currentSlide(1)"></span> 
      <span class="dot" onclick="currentSlide(2)"></span> 
      <span class="dot" onclick="currentSlide(3)"></span> 
      <span class="dot" onclick="currentSlide(4)"></span> 
      <span class="dot" onclick="currentSlide(5)"></span> 
    </div>
  </div>

CSS:

    /* Slideshow container */
    .slideshow-container {
      max-width: 960px;
      position: relative;
      margin: auto;
    }

    /* Next & previous buttons */
    .prev, .next {
      cursor: pointer;
      position: absolute;
      top: 0;
      top: 50%;
      width: auto;
      margin-top: -22px;
      padding: 16px;
      color: white;
      font-weight: bold;
      font-size: 18px;
      transition: 0.6s ease;
      border-radius: 0 3px 3px 0;
     }

     /* Position the "next button" to the right */
    .next {
      right: 0;
      border-radius: 3px 0 0 3px;
    }

    /* On hover, add a black background color with a little bit see-through */
    .prev:hover, .next:hover {
      background-color: rgba(254,213,221,0.6);
    }

    /* The dots/bullets/indicators */
    .dot {
      cursor:pointer;
      height: 13px;
      width: 13px;
      margin: 0 2px;
      background-color: #fed5dd;
      border-radius: 50%;
      display: inline-block;
      transition: background-color 0.6s ease;
    }

    .active, .dot:hover {
      background-color: #fca1a2;
    }

    /* Fading animation */
    .fade {
      -webkit-animation-name: fade;
      -webkit-animation-duration: 1.5s;
      animation-name: fade;
      animation-duration: 1.5s;
    }

    @-webkit-keyframes fade {
      from {opacity: .4} 
      to {opacity: 1}
    }

    @keyframes fade {
    from {opacity: .4} 
    to {opacity: 1}
    }

...And the Java:

        window.onload = function plusSlides(n) {
  showSlides(slideIndex += n);
}

window.onload = function currentSlide(n) {
  showSlides(slideIndex = n);
}

window.onload = function showSlides(n) {
  var i;
  var slides = document.getElementsByClassName("mySlides");
  var dots = document.getElementsByClassName("dot");
  if (n > slides.length) {slideIndex = 1} 
  if (n < 1) {slideIndex = slides.length}
  for (i = 0; i < slides.length; i++) {
      slides[i].style.display = "none"; 
  }
  for (i = 0; i < dots.length; i++) {
      dots[i].className = dots[i].className.replace(" active", "");
  }
  slides[slideIndex-1].style.display = "block"; 
  dots[slideIndex-1].className += " active";

  var slideIndex = 1;
  showSlides(slideIndex);
}

Thanks for any and all help. If I can provide further info, I am available.

Upvotes: 0

Views: 119

Answers (1)

IngGaberick
IngGaberick

Reputation: 319

Edit: my syntax was wrong on the onload function. I just updated the answer.

at first sight, it seems you are calling a function that doesn't exist yet., just move the showSlides(slideIndex); to the end of the file. Remember JS executes pretty much all at once, a good practice is to place your functions first in the JS file :)

Let us know if this doesn't help.

Edit: (didn't realized you can edit the answer haha)

function plusSlides(n) {
  showSlides(slideIndex += n);
}

function currentSlide(n) {
  showSlides(slideIndex = n);
}

function showSlides(n) {
  var i;
  var slides = document.getElementsByClassName("mySlides");
  var dots = document.getElementsByClassName("dot");
  if (n > slides.length) {slideIndex = 1} 
  if (n < 1) {slideIndex = slides.length}
  for (i = 0; i < slides.length; i++) {
      slides[i].style.display = "none"; 
  }
  for (i = 0; i < dots.length; i++) {
      dots[i].className = dots[i].className.replace(" active", "");
  }
  slides[slideIndex-1].style.display = "block"; 
  dots[slideIndex-1].className += " active";

}

var slideIndex = 1;

window.onload = function(e){ 
  showSlides(slideIndex);
};

Upvotes: 1

Related Questions