ADS

header ads

How to create simple slider using HTML and CSS?


Here in this article I am going to share a simplest way to create a automatic slider using HTML CSS and jQuery.

Step 1: Write HTML code as below:-


<!DOCTYPE html>
<html>
<head>
    <title></title>
    <link rel="stylesheet" type="text/css" href="style.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    <script type="text/javascript" src="slider.js"></script>
</head>
<body>
    <div class="slider">
        <img src="images/1.jpg" class="active">
        <img src="images/2.jpg">
        <img src="images/3.jpg">
        <img src="images/4.jpg">
    </div>
</body>
</html>
  
Step 2: Add some Css as below:-

.slider{
    height350px;
    width960px;
    positionrelative;
    marginauto;       ;
}
img{
    min-width100%;
    max-height100%;
    positionabsolute;
    opacity0;
    transition1s;
}
.active{
    opacity1;
}

Step 3: Now add the little bit jQuery.

function slider() {
    $current=$('.slider img.active');
        $next=$current.removeClass('active').next();
        if($next.length==0){
            $('.slider img:first-child').addClass('active');
        }
        else{
            $next.addClass('active');
        }

}
setInterval(slider,2000);


Now you are all set!

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


Post a Comment

0 Comments