JSFiddle - React, Tailwind, and code Playground

by vandanasrivastava

HTML

<div id="slider">
<div class="wrap"> 
<div class="panel">
panel 1
<img src="https://cdn.pixabay.com/photo/2016/02/28/12/55/boy-1226964_960_720.jpg"/>
</div> 
<div class="panel">
panel 2
<img src="https://cdn.pixabay.com/photo/2016/11/29/12/13/fence-1869401_960_720.jpg" />
</div> 

<div class="panel">
panle 3
<img src="https://cdn.pixabay.com/photo/2021/12/19/03/51/tree-6880117_960_720.jpg" />
</div>
</div>
<div class="previous"></div>
<div class="next"></div>
</div>

<div class="progress_bar">
    <ul> 
        <li></li>
        <li></li>
        <li></li>
    </ul>
</div>

<div class="nav">
<ul class="dots">
<li> panel1</li>
<li> panel2</li>
<li> panel3</li>
<ul>
</div>

CSS

#slider { 
    
    position:relative;
    width:400px;
    height:200px;
    background:#CCCCCC;
}
.dots {
  position: absolute;
}
 #slider .panel {
    position:absolute;
    width:400px;
    height:200px;  
    display:none;
}

 #slider .panel:first-child{
    display:block;
}

#slider .previous {
    position:absolute;
    left:0;
    top:100px;
    width:20px;
    height:20px;
    background:#333;
}

#slider .next{
    position:absolute;
    right:0;
    top:100px;
    width:20px;
    height:20px;
    background:#333;
}


.progress_bar { 
 width:0px;
height:25px;
 position:relative;
    background:#fc3;    
}

.progress_bar ul li {
    width:50px;
    height:5px;    
    display:inline-block;
    background:#333333;
}

.nav { margin:5px 0 }
.nav ul li {
    display:inline-block;
    padding:5px;
    background:#ccc;
    
}
img {
  max-width: 100%
}

JavaScript

var currentIndex = 0;// store current pane index displayed
var ePanes = $('#slider .panel'), // store panes collection
    time   = 5000,
    bar = $('.progress_bar');

function showPane(index){// generic showPane
    // hide current pane
    ePanes.eq(currentIndex).stop(true, true).fadeOut();
    // set current index : check in panes collection length
    currentIndex = index;
    if(currentIndex < 0) currentIndex = ePanes.length-1;
    else if(currentIndex >= ePanes.length) currentIndex = 0;
    // display pane
    ePanes.eq(currentIndex).stop(true, true).fadeIn();
    // menu selection
    $('.nav li').removeClass('current').eq(currentIndex).addClass('current');
}
// bind ul links
$('.nav li').click(function(ev){    showPane($(this).index());});
// bind previous & next links
$('.previous').click(function(){    showPane(currentIndex-1);});
$('.next').click(function(){    showPane(currentIndex+1);});
// apply start pane
showPane(0);

function run(){
    bar.width(0);
    showPane(currentIndex+1);
    bar.animate({'width': "+=400"}, time, run);
}

run();