0

I'm trying to make an automatic slideshow that covers the whole page and changes photos every 4 seconds, i have this codepen set up for testing but i have no clue whats wrong with it. There is 5 images im currently using and i want it to just change every couple of seconds if possible, I am using bootstrap 3 on the ASP.NET project but it doesn't interfere with anything i do to the slideshow

https://codepen.io/xElectric/pen/poomKQw

var slideIndex = 0;
showSlides();

function showSlides() {
  var i;
  var slides = document.getElementsByClassName("mySlides");
  var dots = document.getElementsByClassName("dot");
  for (i = 0; i < slides.length; i++) {
    slides[i].style.display = "none";  
  }
  slideIndex++;
  if (slideIndex > slides.length) {slideIndex = 1}    
  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";
  setTimeout(showSlides, 2000); // Change image every 2 seconds
}
* {box-sizing: border-box;}
body {font-family: Verdana, sans-serif;}
.mySlides {display: none;}
img {}

/* Slideshow container */
.slideshow-container {
  position: relative;
  margin: auto;
}


.active {
  background-color: #717171;
}

/* 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}
}

/* On smaller screens, decrease text size */
@media only screen and (max-width: 300px) {
  .text {font-size: 11px}
}
<div style="max-height: 100vh; overflow: hidden;">
        <div class="slideshow-container">

            <div class="mySlides fade">
                <img src="https://i.imgur.com/vX8LONV.jpg" style="width:100vw;">
            </div>

            <div class="mySlides fade">
                <img src="https://i.imgur.com/a9bx26U.jpg" style="width:100vw;">
            </div>

            <div class="mySlides fade">
                <img src="https://i.imgur.com/PijASuc.jpg" style="width:100vw;">
            </div>
            <div class="mySlides fade">
                <img src="https://i.imgur.com/vX8LONV.jpg" style="width:100vw;">
            </div>
            <div class="mySlides fade">
                <img src="https://i.imgur.com/c8AjEXg.jpg" style="width:100vw;">
            </div>
          <div class="mySlides fade">
                <img src="https://i.imgur.com/srhebZH.jpg" style="width:100vw;">
            </div>

        </div>
    </div>
  • The error message means the list of "dot" elements is empty. There are no elements with class="dot" in the source. – Mr Lister Nov 25 '19 at 11:53
  • @Matt.Hamer5 Is it possible to make it automatic with the carousel? – Lukasz Humeniuk Nov 25 '19 at 11:57
  • Fix with @MrLister solution for you to see: https://jsfiddle.net/5kg14cr8/ – MattHamer5 Nov 25 '19 at 11:57
  • instead of nesting the `setTimeout`, it would be much much better to use a single `setInterval`. – Ed Knowles Nov 25 '19 at 12:00
  • @Matt.Hamer5 thanks, but i think that making the carousel will make it look nicer. after its fully automatic – Lukasz Humeniuk Nov 25 '19 at 12:01
  • @LukaszHumeniuk did you get, what was the issue? in the JS code you are trying to access `dot` class elements which are absent in provided HTML code. that was breaking snippet. – Harish Nov 25 '19 at 12:03
  • @LukaszHumeniuk No worries, the BS carousel is already automatic; and you can just delete the relevant HTML to remove the dots and other indicators if needed. Have a look at **[This SO Post](https://stackoverflow.com/questions/17332431/how-can-i-control-the-speed-that-bootstrap-carousel-slides-in-items)** to see how to speed up the slides. – MattHamer5 Nov 25 '19 at 12:04
  • 1
    @Harish I forgot to get rid of the get dots in js which was causing it to break and also setInterval instead of setTimeout fixed it, Thank you everyone for the help – Lukasz Humeniuk Nov 25 '19 at 12:05

1 Answers1

0

You wrote some useless code in you snippet, which you don't need like dot, dot div you don't have in your HTML code,

var slideIndex = 0;
showSlides();

function showSlides() {
  var i;
  var slides = document.getElementsByClassName("mySlides");
  var dots =   document.getElementsByClassName("dot");

  for (i = 0; i < slides.length; i++) {
    slides[i].style.display = "none";  
  }

  slideIndex++;
  
  if (slideIndex > slides.length) {slideIndex = 1}

  slides[slideIndex-1].style.display = "block";  
  setTimeout(showSlides, 2000); // Change image every 2 seconds
}
* {box-sizing: border-box;}
body {font-family: Verdana, sans-serif;}
.mySlides {display: none;}
img {}

/* Slideshow container */
.slideshow-container {
  position: relative;
  margin: auto;
}


.active {
  background-color: #717171;
}

/* 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}
}

/* On smaller screens, decrease text size */
@media only screen and (max-width: 300px) {
  .text {font-size: 11px}
}
<div style="max-height: 100vh; overflow: hidden;">
        <div class="slideshow-container">

            <div class="mySlides fade">
                <img src="https://i.imgur.com/vX8LONV.jpg" style="width:100vw;">
            </div>

            <div class="mySlides fade">
                <img src="https://i.imgur.com/a9bx26U.jpg" style="width:100vw;">
            </div>

            <div class="mySlides fade">
                <img src="https://i.imgur.com/PijASuc.jpg" style="width:100vw;">
            </div>
            <div class="mySlides fade">
                <img src="https://i.imgur.com/vX8LONV.jpg" style="width:100vw;">
            </div>
            <div class="mySlides fade">
                <img src="https://i.imgur.com/c8AjEXg.jpg" style="width:100vw;">
            </div>
          <div class="mySlides fade">
                <img src="https://i.imgur.com/srhebZH.jpg" style="width:100vw;">
            </div>

        </div>
  
  
    </div>

if you need any help please inform us!

Happy codin'!

Ericgit
  • 6,089
  • 2
  • 42
  • 53