'Dynamically load images from folder for slideshow

my task is to create a webpage containing a slideshow, dynamically getting the displayed images from a folder. For the slideshow I used this "tutorial": https://www.w3schools.com/howto/howto_js_slideshow.asp. I did some modifications based on my needs - works like a charm. The only open task is the part with the dynamic loading of the images. I need every image in a folder to be added to the body, so my js includes the image to the slideshow. Additionally, for every image I want to add another "bullet point", which indicates the current position.

HTML

<!DOCTYPE html>
<html>
<head>
<title>Slideshow</title>
<meta name="viewport" content="width=device-width, initial-scale=1">

<!--Linking CSS-->
<link rel="stylesheet" href="style.css"/>

</head>
<body>

<div class="mySlides fade">
    <img src="images/rick.jpg" style="width:100%">
</div>
      
<div class="mySlides fade">
    <img src="images/rick2.png" style="width:100%">
</div>
      
<div class="mySlides fade">
    <img src="images/rick3.jpg" style="width:100%">
</div>

<div class="mySlides fade">
    <img src="images/rick4.jpg" style="width:100%">
</div>

<div class="mySlides fade">
    <img src="images/rick6.jpg" style="width:100%">
</div>

</div>


<div style="text-align:center">
  <span class="dot"></span> 
  <span class="dot"></span> 
  <span class="dot"></span> 
  <span class="dot"></span>
  <span class="dot"></span>
</div>

<!--Linking JS-->
<script src="script.js"></script>


</body>
</html> 

CSS

* {box-sizing: border-box;}
* {margin: 0; padding: 0;}
body {
  overflow: hidden; /* hiding scrollbars */
  font-family: Verdana, sans-serif;
}

img {vertical-align: middle;}


.mySlides {
    background-size: cover;
    background-repeat: no-repeat;
    height: 100vh;
}

/* The dots/bullets/indicators */
.dot {
  height: 15px;
  width: 15px;
  margin: 0 2px; /* distance between dots */
  background-color: #bbb; /* maybe change to RS */
  display: inline-block; /* inline-block */
  position: relative; top:-70px;
  border-radius: 50%;
 
  transition: background-color 0.6s ease;
}

.active {
  background-color: #717171;
}

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

@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}
}

JAVASCRIPT

let slideIndex = 0;
showSlides();

//Function to make automatic slideshow
function showSlides() {
  let i;
  let slides = document.getElementsByClassName("mySlides");
  let 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, 20000); // Change image every 2 seconds
}

The goal is to have a running webpage with the slideshow, so when a person adds a new image to the images folder, I do not have to add another

<div class="mySlides fade">
    <img src="images/image.xy" style="width:100%">
</div>

and another

<span class="dot"></span> 

into the <div style="text-align:center"> </div>

Any help to achieve this is highly appreciated. Thank you!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source