How do I show png images based on their names in javascript?

poromazh

I have a folder with png images and several other types of files. I only want to display the png images in the order of their names, how can I do that? All images end in a number; for example, each image is titled "image_001", "image_002", and so on. Right now I have all the images grouped together in a class as shown below but I'd prefer not to have to add every individual image if I didn't want to include any other file types. Thank you in advance.

 <section>
        <img class="pics" src="imgfolder/picture_001.png" style="width:80%">
        <img class="pics" src="imgfolder/picture_002.png" style="width:80%">
        <img class="pics" src="imgfolder/picture_003.png" style="width:80%">
 </section>

<script type="text/javascript">
        var index = 0;
        change();

        function change() {
             var images = document.getElementsByClassName('pics');

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

             if(index > images.length) { 
                 index = 1; 
             }

             images[index - 1].style.display = "block";

             setTimeout(change, 3000);
         }
</script>
Samathingamajig

The JS code is commented with what it does. I've tested this with the same file structure that you used in your question, but you can change it on JS line 9.

<section id="img-container"></section>
const numOfPictures = 3; // The number of pictures in folder
const picturesNumberLength = 3; // "000"
let imageIndex = 1;
let imagesArray = [];
const imagesContainer = document.getElementById("img-container"); // Get the images container, has id "img-container"

for (let i = 1; i < numOfPictures + 1; i++) { // Starts at a 1 index "001"
  const img = document.createElement("img"); // Create an image element
  img.src = `imgfolder/picture_${(i+"").padStart(picturesNumberLength,"0")}.png`; // Set the source to "imgfolder/picture_001" or other number, works up to 999
  img.classList.add("pics"); // Add the pics class
  img.style.width = "80%"; // Sets width to 80%
  img.style.display = "none"; // Turns off displaying it
  imagesContainer.appendChild(img); // Puts the image in the image container
  imagesArray.push(img); // Push the reference to the array
}
imagesArray[0].style.display = "block"; // Display the first block
setInterval(() => { // Every 3000ms (3secs), do this
  imagesArray[imageIndex].style.display = "block"; // Turn displaying on
  if (imageIndex > 0) imagesArray[imageIndex-1].style.display = "none"; // Turn the previous one off
  else imagesArray[numOfPictures-1].style.display = "none";
  imageIndex++; // Change the index
  if (imageIndex >= numOfPictures) imageIndex = 0; // Go back to the beginning after going to the end
}, 3000);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I to show images before upload?

From Dev

Exporting Excel tables to PNG images - how do I increase resolution?

From Dev

How do I convert a TTF into individual PNG character images?

From Dev

Haxe with swf target how do I embed PNG images

From Dev

PHP GD - How do I create a new png image from 3 png images in layers in order

From Dev

How do I change all images with the name x.png with x.png with Tampermonkey or Stylish?

From Dev

How do I resize images in javascript/html

From Dev

How do i show images of all installed apps in android?

From Dev

How do i create graphs and images to show on the same panel in R

From Dev

How do I get Thunderbird to always show embedded images?

From Dev

Flutter - How do I Show Images , after every 5 items?

From Dev

How do I subset columns in R based on the length of the column names?

From Dev

How do I subset a dataset based on variable names using a loop?

From Dev

Running Flask locally. I want to display images based on names generated in JavaScript

From Dev

How do I convert RGBA raw buffer to PNG file in Javascript?

From Dev

How to load .png images with image names listed in a .csv file to R

From Javascript

How can I display images based on the title of the image in javascript?

From Dev

How do I show navbar items based on a model field in Django?

From Dev

How do I create show page based on id of item clicked

From Javascript

How do I toggle an ng-show in AngularJS based on a boolean?

From Dev

How do I show only the rows based on the value in the condition?

From Dev

How do I show Console.log based on state?

From Dev

How do I show a DIV id based on three inputs?

From Dev

how do I merge 2 images with the same file names into one in a batch process? Is there a prgram that can do this?

From Dev

How to show a combined image of two resource png images in android

From Dev

Javascript - How do I map an array of objects to a datatable with column names?

From Dev

How do I save the names and values of inputs in the page to an object in javascript?

From Dev

How do I Center Images for a Javascript Slideshow when the width is unknown?

From Dev

How do I add images in this select list? Javascript?

Related Related

  1. 1

    How do I to show images before upload?

  2. 2

    Exporting Excel tables to PNG images - how do I increase resolution?

  3. 3

    How do I convert a TTF into individual PNG character images?

  4. 4

    Haxe with swf target how do I embed PNG images

  5. 5

    PHP GD - How do I create a new png image from 3 png images in layers in order

  6. 6

    How do I change all images with the name x.png with x.png with Tampermonkey or Stylish?

  7. 7

    How do I resize images in javascript/html

  8. 8

    How do i show images of all installed apps in android?

  9. 9

    How do i create graphs and images to show on the same panel in R

  10. 10

    How do I get Thunderbird to always show embedded images?

  11. 11

    Flutter - How do I Show Images , after every 5 items?

  12. 12

    How do I subset columns in R based on the length of the column names?

  13. 13

    How do I subset a dataset based on variable names using a loop?

  14. 14

    Running Flask locally. I want to display images based on names generated in JavaScript

  15. 15

    How do I convert RGBA raw buffer to PNG file in Javascript?

  16. 16

    How to load .png images with image names listed in a .csv file to R

  17. 17

    How can I display images based on the title of the image in javascript?

  18. 18

    How do I show navbar items based on a model field in Django?

  19. 19

    How do I create show page based on id of item clicked

  20. 20

    How do I toggle an ng-show in AngularJS based on a boolean?

  21. 21

    How do I show only the rows based on the value in the condition?

  22. 22

    How do I show Console.log based on state?

  23. 23

    How do I show a DIV id based on three inputs?

  24. 24

    how do I merge 2 images with the same file names into one in a batch process? Is there a prgram that can do this?

  25. 25

    How to show a combined image of two resource png images in android

  26. 26

    Javascript - How do I map an array of objects to a datatable with column names?

  27. 27

    How do I save the names and values of inputs in the page to an object in javascript?

  28. 28

    How do I Center Images for a Javascript Slideshow when the width is unknown?

  29. 29

    How do I add images in this select list? Javascript?

HotTag

Archive