ADS

header ads

How to create a manual slider using HTML and CSS?

Step 1: Write HTML code as below:-

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="mainContainer">
    <div class="slideShowContainer">
        <div class="imgContainer">
            <img src="images/1.jpg">
        </div>
        <div class="imgContainer">
            <img src="images/2.jpg">
        </div>
        <div class="imgContainer">
            <img src="images/3.jpg">
        </div>
        <div class="imgContainer">
            <img src="images/4.jpg">
        </div>
        <div onclick="plusSlides(-1)" class="leftArrow">&#10094</div>
        <div onclick="plusSlides(1)" class="rightArrow">&#10095</div>
    </div>
    <script src="main.js"></script>
</div>
</body>
</html>
  
Step 2: Add some Css as below:-

.mainContainer{
    width100%;
    height400px;
    max-width1000px;
    marginauto;
    box-shadow0px 0px 3px 1px #00000078;
    padding10px;
}
.slideShowContainer{
    width100%;
    height100%;
    overflowhidden;
    positionrelative;
}
.imgContainer{
    width100%;
    height100%;
    positionabsolute;
    opacity0;
}
.imgContainer img{
    width100%;
    height100%;
}
.leftArrow,.rightArrow{
    width50px;
    background#1d090936;
    positionabsolute;
    color#ffff;
    text-aligncenter;
    padding-top:30px;
    z-index1;
    height50px;
    top40%;
    user-selectnone;
}
.rightArrow{
    right0;
}
.leftArrow:hover,.rightArrow:hover{
    background#000000a8;
    cursorpointer;
}
/*Moving the slider to the left side*/
.mainContainer .moveLeftCurrentSlide{
    animation-name: moveLeftCurrent;
    animation-duration0.5s;
    animation-fill-mode:forwards;
}
.mainContainer .moveLeftNextSlide{
    animation-name: moveLeftNext;
    animation-duration0.5s;
    animation-fill-mode:forwards;
}
@keyframes moveLeftCurrent {
    from {margin-left0;opacity1;}
    to {margin-left-100%;opacity1;}
}
@keyframes moveLeftNext {
    from {margin-left100%;opacity1;}
    to {margin-left0%;opacity1;}
}

/*moving right side the current slider*/
.mainContainer .moveRightCurrentSlide{
    animation-name: moveRightCurrent;
    animation-duration0.5s;
    animation-timing-functionlinear;
    animation-fill-mode:forwards;
}
.mainContainer .moveRightPrevSlide{
    animation-name: moveRightPrev;
    animation-duration0.5s;
    animation-timing-functionlinear;
    animation-fill-mode:forwards;
}
@keyframes moveRightCurrent {
    from {margin-left0;opacity1;}
    to {margin-left100%;opacity1;}
}
@keyframes moveRightPrev {
    from {margin-left-100%;opacity1;}
    to {margin-left0%;opacity1;}
}

Step 3: Now add the little bit jQuery.

var slideIndex,slides;
function slider(){
    slides=document.getElementsByClassName("imgContainer");
    slideIndex = 0;
    slides[slideIndex].style.opacity=1;
}
slider();
function plusSlides(n) {
    moveSlide(slideIndex+n);
}
function moveSlide(n){
    var i,current,next,forNext,forCurrent;
    if(n>slideIndex) {
        if(n >= slides.length)
        {
            n=0;
        }
        forCurrent="moveLeftCurrentSlide";
        forNext="moveLeftNextSlide";
     }
    else if(n<slideIndex){
        if(n<0)
        {
            n=slides.length-1;
        }
        forCurrent="moveRightCurrentSlide";
        forNext="moveRightPrevSlide";
    }
        next = slides[n];
        current=slides[slideIndex];
        for (i = 0i < slides.lengthi++) {
            slides[i].className ="imgContainer";
            slides[i].style.opacity=0;
        }
        current.classList.add(forCurrent);
        next.classList.add(forNext);
        slideIndex=n;
}


Now you are all set!

If you want to download this project click on the link below:-

Post a Comment

0 Comments