Skip to main content

Creating custom carousel using HTML, CSS & Vanilla Javascript

Create a custom carousel with fading effect using HTML, CSS & Vanilla Javascript. I have used the Font Awesome classes for control icons. There is some delay switching between the images when using controls.

HTML:-
<div class="carousel" data-interval-time="6500">
                <ul>
                    <li><img src="img/image1.jpg" alt="CarouselImage_1"/></li>
                    <li><img src="img/image2.jpg" alt="CarouselImage_2"/></li>
                    <li><img src="img/image3.jpg" alt="CarouselImage_3"/></li>
                    <li><img src="img/image4.jpg" alt="CarouselImage_4"/></li>
                    <li class="saver"></li>
                </ul>
                <div class="controls">
                    <ul>
                        <li class="img-prev"><span class="fa fa-backward"></span></li>
                        <li class="img-state"><span class="fa fa-pause" data-play-state="playing"></span></li>
                        <li class="img-next"><span class="fa fa-forward"></span></li>
                    </ul>
                </div>
                <div class="progress"></div>
</div>

CSS:-

.carousel{
    position: absolute;
    display: block;
    width: 100%;
    height: 390px;
    max-height: 410px;
    background: rgba(65, 65, 65, 1);
    padding: 1%;
    overflow: hidden;
}
.carousel>.progress{
    position: absolute;
    background: rgba(255, 255, 255, 1);
    width: 0;
    height: 5px;
    bottom: -15px;
    border-radius: 10px;
    margin-left: -1%;
    z-index: 1;
}
.carousel>.controls{
    position: absolute;
    background: rgba(0, 0, 0, 1);
    width: auto;
    max-width: 150px;
    border-radius: 10px;
    color: #FFF;
    bottom: 30px;
    z-index: 12;
    opacity: 0.25;
    transition: opacity 0.25s 0s ease;
    transition-property: all;
}
.carousel>.controls:hover{
    opacity: 1;
}
.carousel>.controls>ul{
    list-style: none;
}
.carousel>.controls>ul>li{
    padding: 10px;
    display: inline-block;
    transition: background 0.25s 0s ease;
    transition-property: all;
    cursor: pointer;
}
.carousel>.controls>ul>li:hover{
    background: rgba(80, 80, 80, 1);
    border-radius: 10px;
}
.carousel>ul{
    list-style: none;
}
.carousel>ul>li{
    position: absolute;
    width: 55%;
}
.carousel>ul>li>img{
    position: absolute;
    width: 100%;
    height: auto;
    min-height: 350px;
    max-height: 350px;
    border-radius: 10px;
    opacity: 1;
    transition: opacity 0.65s 0s ease;
    transition-property: all;
}


Javascript:-

/**
 * This variable will store index of the current image shown on the carousel
 */
var currentIndex = 0;
/**
 * This variable will store index of the next image to show on the carousel
 */
var newIndex = 0;
/**
 * This variable will store the intervalID for the carousel. It is used to move forward, move backward and to pause or play the carousel
 */
var interval;
function carousel() {
    var images = $(".carousel").getElementsByTagName("img");
    var intval = $(".carousel").getAttribute("data-interval-time");
    $(".carousel .progress").style.transition = "width " + (intval / 1000) + "s 0s ease";
    $(".carousel .progress").style.transitionProperty = "all";
    $(".carousel .progress").style.width = "100%";
    newIndex = images.length - 1;
    images[currentIndex].style.opacity = "1";
    images[newIndex].style.opacity = "0";
    interval = setInterval(function () {
        switchImages(images);
    }, intval);
    $(".carousel .controls .img-next").onclick = function () {
        clearInterval(interval);
        currentIndex = newIndex;
        images[currentIndex].style.opacity = "0";
        newIndex++;
        newIndex = newIndex == images.length ? 0 : newIndex;
        images[newIndex].style.opacity = "1";
        interval = setInterval(function () {
            switchImages(images);
        }, intval);
    };
    $(".carousel .controls .img-prev").onclick = function () {
        clearInterval(interval);
        images[currentIndex].style.opacity = "0";
        newIndex = currentIndex - 1;
        newIndex = newIndex == -1 ? images.length - 1 : newIndex;
        currentIndex = newIndex;
        images[newIndex].style.opacity = "1";
        newIndex++;
        newIndex = newIndex == images.length ? 0 : newIndex;
        interval = setInterval(function () {
            switchImages(images);
        }, intval);
    };
    $(".carousel .controls .img-state").onclick = function () {
        var state = $(".carousel .controls .img-state span").getAttribute("data-play-state");
        if (state === "playing") {
            clearInterval(interval);
            $(".carousel .controls .img-state span").setAttribute("data-play-state", "paused");
            $(".carousel .controls .img-state span").setAttribute("class", "fa fa-play");
        }
        if (state === "paused") {
            if (interval) {
                clearInterval(interval);
            }
            interval = setInterval(function () {
                switchImages(images);
            }, intval);
            $(".carousel .controls .img-state span").setAttribute("data-play-state", "playing");
            $(".carousel .controls .img-state span").setAttribute("class", "fa fa-pause");
        }
    };
}
/**
 * This function switches between the images after a certain time interval.
 * @param {Array} list An array of DOMObject of the images to switch between.
 */
function switchImages(list) {
    $(".carousel .progress").style.width = $(".carousel .progress").style.width == "0px" ? "100%" : "0";
    list[currentIndex].style.opacity = "0";
    list[newIndex].style.opacity = "1";
    currentIndex = newIndex;
    newIndex++;
    newIndex = newIndex == list.length ? 0 : newIndex;
}

Comments

Popular posts from this blog

Java Program to calculate the Run Rate per over in a cricket match

import java.io.*; import java.util.*; public class RunRate{     Scanner scan=new Scanner(System.in);     int runs, balls;     float runRate;     public void input(){         try{             System.out.println("Enter Runs Scored: ");             runs=scan.nextInt();             System.out.println("Enter Balls Delivered: ");             balls=scan.nextInt();         }         catch(NumberFormatException e){             System.out.println("Error Code: "+e);             System.exit(0);   ...

Java Program to calculate the Strike Rate of a Cricket Batsman

import java.io.*; import java.util.*; public class StrikeRate{     Scanner scan=new Scanner(System.in);     int ballsFaced, runs;     double strikeRate;     public void input(){         try{             System.out.println("Enter Runs Scored: ");             runs=scan.nextInt();             System.out.println("Enter Balls Faced: ");             ballsFaced=scan.nextInt();         }         catch(NumberFormatException e){             System.out.println("Error Code: "+e);             System.exit(0); ...

WP similar or related posts widget without using any plugin

Hi guys, Recently I was working on some WP website and my client told me that he required a widget for displaying related/similar posts on the single post page. But as his website was already using many plugins, even for some pretty small tasks like this one, I decided not to use another WP plugin (plugins are not good for your WP websites, we will discuss about that on some other post.) I am not explaining the code as it is pretty simple if you are familiar with WP classes. But please let me know if you have any questions related to the PHP code posted below in the comments section or even much better, on Gist. You can add the following code directly in your child theme's functions.php file or you can create a separate file and include this at the bottom of functions.php file. <?php class similar_posts_widget extends WP_Widget {     function __construct()     {         parent::__construct('similar_posts_...