Automatic Image Slider using JavaScript Last Updated : 26 Jul, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In this article, we will discuss how to create an automatic image slider using JavaScript. An image slider is a popular component of modern websites that allows the display of multiple images in a single location, usually in a sliding motion. With the use of JavaScript, creating an automatic image slider has become much simpler. Preview:Approach:The first step is to create the HTML markup for the image slider. The markup will contain an unordered list (<ul>) that will hold all the images. Each list item (<li>) will have an image and a caption.Now, grab the HTML elements using their classes and IDs to style them using CSS styling.Select the slider element and all the slide elements using the querySelectorAll() method. Store the total number of slides in the slideCount variable and set the initial active slide to 0.Use the setInterval() method to execute the anonymous function every 5000 milliseconds (or 5 seconds). Inside the function, we remove the 'active' class from the current slide and increment the activeSlide variable by 1. If the activeSlide variable is equal to the slideCount, we reset it back to 0.Example: The below example explains how you can use JavaScript to create an automatic image slider. 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"> <title>Image Slider</title> <link rel="stylesheet" href="style.css"> </head> <body> <center> <div class="slider"> <ul> <li> <img src= "https://media.geeksforgeeks.org/wp-content/uploads/20230329113637/image1.png" alt="GFG Image 1"> <div class="caption"> Geeks for Geeks </div> </li> <li> <img src= "https://media.geeksforgeeks.org/wp-content/uploads/20230329113637/image2.jpeg" alt="GFG Image 2"> <div class="caption"> Geeks for Geeks </div> </li> <li> <img src= "https://media.geeksforgeeks.org/wp-content/uploads/20230329113638/image3.jpeg" alt="GFG Image 3"> <div class="caption"> Geeks for Geeks </div> </li> <li> <img src= "https://media.geeksforgeeks.org/wp-content/uploads/20230329113638/image4.jpeg" alt="GFG Image 4"> <div class="caption"> Geeks for Geeks </div> </li> </ul> </div> </center> <script src="script.js"></script> </body> </html> CSS .slider { width: 100%; position: relative; } .slider ul { list-style: none; margin: 0; padding: 0; } .slider li { position: absolute; top: 0; left: 0; opacity: 0; transition: opacity 1s ease-in-out; } img { height: 400px; width: 400px; } .slider li.active { opacity: 1; } .slider .caption { position: absolute; bottom: 0; left: 0; width: 100%; background-color: rgba(0, 0, 0, 0.5); color: #fff; font-size: 18px; padding: 10px; text-align: center; } JavaScript const slider = document.querySelector('.slider'); const slides = slider.querySelectorAll('li'); // Store the total number of images const slideCount = slides.length; let activeSlide = 0; // To change the images dynamically every // 5 Secs, use SetInterval() method setInterval(() => { slides[activeSlide].classList.remove('active'); activeSlide++; if (activeSlide === slideCount) { activeSlide = 0; } slides[activeSlide].classList.add('active'); }, 2000); Output: Comment More infoAdvertise with us Next Article Automatic Image Slider using JavaScript N nishth7hs8 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Projects Similar Reads Creating a Custom Image Slider using JavaScript An Image Slider or Image Carousel is a popular feature on websites that allows us to display multiple images within a limited space. It helps showcase attractive and dynamic visuals to engage visitors effectively.Steps To Creating a Custom Image Slider using JavaScriptThese are the some steps which 3 min read Creating a Custom Image Slider using JavaScript An Image Slider or Image Carousel is a popular feature on websites that allows us to display multiple images within a limited space. It helps showcase attractive and dynamic visuals to engage visitors effectively.Steps To Creating a Custom Image Slider using JavaScriptThese are the some steps which 3 min read Creating a Custom Image Slider using JavaScript An Image Slider or Image Carousel is a popular feature on websites that allows us to display multiple images within a limited space. It helps showcase attractive and dynamic visuals to engage visitors effectively.Steps To Creating a Custom Image Slider using JavaScriptThese are the some steps which 3 min read How to create image slider using HTML CSS and JavaScript ? An image slide, or slideshow, is a dynamic display of images that automatically transitions from one to the next, often with animations. To create an image slide, use HTML to structure the images, CSS for styling and animations, and JavaScript to control the timing and transitions between images.App 3 min read script.aculo.us Sliders range Option The script.aculo.us library is a cross-browser library that aims at improving the user interface of a website. The Slider controls are thin tracks that allow the user to input values. It is done by defining a range of values that can be selected by the user by dragging the handle to the appropriate 2 min read Like