Creating a Custom Image Slider using JavaScript
Last Updated :
31 Jul, 2024
What is an image slider?
An image Slider or Image Carousel is an expedient way to show multiple images on a website. Alluring flashy images can draw many visitors to the site. The below image shows a sample image slider:
In this post, we will create the above image slider using HTML, CSS and JavaScript. Let's begin with creating the image slider.
Step - 1: To create the structure for the image slider using HTML we will use a parent div called container and inside that div, we will insert all the images from the respective sources.
Below is the complete HTML code for this approach:
html
<!DOCTYPE html>
<html>
<body>
<!--HTML Code-->
<!-- Slideshow Container Div -->
<div class="container">
<!-- Full-width images with caption text -->
<div class="image-sliderfade fade">
<img src="img1.jpg"
style="width: 100%" />
<div class="text">Image caption 1</div>
</div>
<div class="image-sliderfade fade">
<img src="img2.jpg"
style="width: 100%" />
<div class="text">Image caption 2</div>
</div>
<div class="image-sliderfade fade">
<img src="img3.jpg"
style="width: 100%" />
<div class="text">Image caption 3</div>
</div>
<div class="image-sliderfade fade">
<img src="img3.jpg"
style="width: 100%" />
<div class="text">Image caption 4</div>
</div>
</div>
<br />
<!-- The dots/circles -->
<div style="text-align: center">
<span class="dot"></span>
<span class="dot"></span>
<span class="dot"></span>
</div>
</body>
</html>
Step - 2: Once we have created the HTML structure for our image slider, the next step is to style the slider using CSS. We will add styles to the images, backgrounds etc. We will also style the dots and make our images responsive and browser-friendly using CSS. Below is the complete CSS code for styling our image slider:
css
// CSS code
* {
box-sizing: border-box;
}
body {
font-family: Verdana, sans-serif;
}
.image-sliderfade {
display: none;
}
img {
vertical-align: middle;
}
/* Slideshow container */
.container {
max-width: 1000px;
position: relative;
margin: auto;
}
/* Caption text */
.text {
color: #f2f2f2;
font-size: 15px;
padding: 20px 15px;
position: absolute;
right: 10px;
bottom: 10px;
width: 40%;
background: rgba(0, 0, 0, 0.7);
text-align: left;
}
/* The dots/bullets/indicators */
.dot {
height: 15px;
width: 15px;
margin: 0 2px;
background-color: transparent;
border-color: #ddd;
border-width: 5 px;
border-style: solid;
border-radius: 50%;
display: inline-block;
transition: border-color 0.6s ease;
}
.active {
border-color: #666;
}
/* Animation */
.fade {
-webkit-animation-name: fade-image;
-webkit-animation-duration: 1.5s;
animation-name: fade-image;
animation-duration: 1.5s;
}
@-webkit-keyframes fade-image {
from {
opacity: 0.4;
}
to {
opacity: 1;
}
}
@keyframes fade-image {
from {
opacity: 0.4;
}
to {
opacity: 1;
}
}
/* On smaller screens, decrease text size */
@media only screen and (max-width: 300px) {
.text {
font-size: 11px;
}
}
Step - 3: After adding styles to the slider, the last thing left is to use javascript to add the functionality of auto changing of images after a specific time interval. In the code snippet below, at the very beginning, we took all the div elements with class name 'image-sliderfade' in an array and did the same for divs with class name 'dots' by using getElementByClassName() listener. After that, we set the display for all of the divs, containing images. In the last for-loop, we have erased the class name for each element of array dots[].
After we are done with these, we set the display as a block of the image to be shown and append the class name to the corresponding element of the dots[] array. Function setTimeout is used for calling the function showslides() in an interval of 2 seconds.
Below is the complete JavaScript code:
javascript
let slideIndex = 0;
showSlides(); // call showslide method
function showSlides() {
let i;
// get the array of divs' with classname image-sliderfade
let slides = document.getElementsByClassName("image-sliderfade");
// get the array of divs' with classname dot
let dots = document.getElementsByClassName("dot");
for (i = 0; i < slides.length; i++) {
// initially set the display to
// none for every image.
slides[i].style.display = "none";
}
// increase by 1, Global variable
slideIndex++;
// check for boundary
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";
// Change image every 2 seconds
setTimeout(showSlides, 2000);
}
Once we are done with all of the above steps, we will get the slider up and working as shown below:
Similar Reads
Creating a Simple Image Editor using JavaScript In this article, we will be creating a Simple Image Editor that can be used to adjust the image values like brightness, contrast, hue, saturation, grayscale, and sepia. Image editors allow one to quickly edit pictures after they have been captured for enhancing them or completely changing their look
10 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
Creating A Range Slider in HTML using JavaScript Range Sliders are used on web pages to allow the user specify a numeric value which must be no less than a given value, and no more than another given value. That is, it allows one to choose the value from a range which is represented as a slider. A Range slider is typically represented using a slid
3 min read
Automatic Image Slider using JavaScript 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 s
3 min read
How to Create an Image Element using JavaScript? Creating an image element dynamically using JavaScript is a useful technique when you want to add images to a webpage without having to manually write the <img> tag in your HTML. This allows for more flexibility, as we can create and manipulate images based on user interactions or other condit
2 min read
How to Create Image Gallery using JavaScript? An Image Gallery is a collection of images displayed in a grid or slideshow format on a webpage. To create an Image Gallery using JavaScript, you can dynamically load images, create HTML elements, and use CSS for styling. JavaScript can add interactivity, like transitions and navigation controls.Her
3 min read
How to Create Image Lightbox Gallery using HTML CSS and JavaScript ? A lightbox gallery is basically used to view the gallery images in detail specifically. You can code the JavaScript to do so but also we can use some downloaded JS and CSS. In this article, we will download the lightbox and attach the JS and CSS files into our code. For our own design satisfaction,
3 min read
Random Image Generator using JavaScript In this article, we will learn how to generate random images in fixed intervals using only HTML, CSS, and JavaScript.Approach: The pictures that are going to be generated randomly on the website should be stored in the form of an array; these pictures are then displayed to the user within a regular
4 min read
How to create Image Comparison Slider using CSS ? In this article, we will see how to create an Image Comparison Slider using CSS. The Image Comparison Slider essentially aids in the distinction of two photographs or products. As a result, the user can quickly determine which of the two products or two images in a better way.Approach: The approach
2 min read
Create A Draggable Card Slider in HTML CSS & JavaScript In this article, we will demonstrate how to create a draggable card slider using HTML, CSS, and JavaScript. We'll use a GeeksforGeeks card slider as an example and implement the functionality to slide cards left and right using arrow buttons. Additionally, we'll incorporate the draggable option, all
5 min read