Automatic Image Slider using HTML, CSS & JavaScript

Automatic Image Slider using HTML, CSS & JavaScript

Hello developers, today in this blog, you'll learn how to create an Automatic Image Slider using HTML, CSS & JavaScript. Earlier we have posted the Responsive Automatic Image Slider using HTML & CSS. Now it's time to create an Automatic Image Slider using HTML, CSS & JavaScript.

A slide show is a presentation of a series of images on a device in a prearranged sequence. The images may change automatically at regular intervals, or they may be manually controlled by the presenter. Automatic Slider may be the convenient way to display the changing or sliding to next one by one on your website.

In this blog (Automatic Image Slider) on the webpage, there are three images that will automatically slide one by one, so that the users need not change the images manually to see the next image.

In this Responsive Automatic Image Slider, you can give the image as much you want. JavaScript code is used to make the image slide automatically.

The source code of this Automatic Image Slider using HTML, CSS & JavaScript is given below, if you want the source code of this program, you can copy it. You can use this code of Automatic Image Slider with your creativity and can take this card to the next level.

Automatic Image Slider using HTML, CSS & JavaScript [Source Code]

To make this website, you would like to make three files: an HTML file, a CSS file & a JavaScript file. First, create an HTML file with the name of index.html and remember, you have to create a file with a .html extension.

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="style.css"> <title>Automatic image Silder || Learning Robo</title> </head> <body> <div id="slidy-container"> <figure id="slidy"> <img src="https://cdn.pixabay.com/photo/2021/08/25/20/42/field-6574455__340.jpg" alt="eyes" > <img src="https://cdn.pixabay.com/photo/2021/08/30/21/29/port-6587129__340.jpg" alt="lou" > <img src="https://cdn.pixabay.com/photo/2021/08/26/14/31/flower-6576240__340.jpg" alt="lucie-2" > </figure> </div> <div class="credit">Made with <span style="color:tomato">❤</span> by <a href="https://www.learningrobo.com/">Learning Robo</a></div> <script src="script.js"></script> </body> </html>
CSS provides style to an HTML page. To make the page attractive create a CSS file with the name style.css and remember that you have to make a file with a .css extension.


*{
	margin:0;
	padding:0;
	box-sizing: border-box;
}
body {
    margin: 0;
    background: #11998e;  
  background: -webkit-linear-gradient(to right, #38ef7d, #11998e);  
  background: linear-gradient(to right, #38ef7d, #11998e); 
  }
#slidy-container { 
    width: 90%; 
    height:550px;
    overflow: hidden; 
    margin: 30px auto 0 auto;
    border-radius: 20px;
    box-shadow: 0 10px 20px -5px #00000061;
  }
  #slidy img{
    width: 100%; 
    height:650px; 
  }
  .credit{
    text-align: center;
    color: #000;
    font-family: 'Trebuchet MS', 'Lucida Sans Unicode', 'Lucida Grande', 'Lucida Sans', Arial, sans-serif;
}
.credit a{
    text-decoration: none;
    color:#000;
    font-weight: bold;
} 
JavaScript makes the page work functionally which makes the count-down work. At last, create a JavaScript file with the name of script.js and remember that you've got need to make a file with a .js extension.


var timeOnSlide = 3, 		
timeBetweenSlides = 1,
animationstring = 'animation',
animation = false,
keyframeprefix = '',
domPrefixes = 'Webkit Moz O Khtml'.split(' '), 
pfx  = '',
  slidy = document.getElementById("slidy"); 
  if (slidy.style.animationName !== undefined) 
    { animation = true; } 
  if( animation === false ) {
    for( var i = 0; i < domPrefixes.length; i++ ) {
      if( slidy.style[ domPrefixes[i] + 'AnimationName' ] !== undefined ) {
        pfx = domPrefixes[ i ];
        animationstring = pfx + 'Animation';
        keyframeprefix = '-' + pfx.toLowerCase() + '-';
        animation = true;
        break;
      }
    }
  }
  if( animation === false ) {
  
  } 
  else {
    var images = slidy.getElementsByTagName("img"),
        firstImg = images[0], 
        imgWrap = firstImg.cloneNode(false);  
    slidy.appendChild(imgWrap); 
    var imgCount = images.length, 
        totalTime = (timeOnSlide + timeBetweenSlides) * (imgCount - 1), 
        slideRatio = (timeOnSlide / totalTime)*100,
        moveRatio = (timeBetweenSlides / totalTime)*100, 
        basePercentage = 100/imgCount, 
        position = 0,
        css = document.createElement("style"); 
    css.type = "text/css";
    css.innerHTML += "#slidy { text-align: left; margin: 0; font-size: 0; position: relative; width: " + (imgCount * 100) + "%;  }\n"; 
    css.innerHTML += "#slidy img { float: left; width: " + basePercentage + "%; }\n";
    css.innerHTML += "@"+keyframeprefix+"keyframes slidy {\n"; 
    for (i=0;i<(imgCount-1); i++) { 
      position+= slideRatio; 
      css.innerHTML += position+"% { left: -"+(i * 100)+"%; }\n";
      position += moveRatio; 
      css.innerHTML += position+"% { left: -"+((i+1) * 100)+"%; }\n";
  }
    css.innerHTML += "}\n";
    css.innerHTML += "#slidy { left: 0%; "+keyframeprefix+"transform: translate3d(0,0,0); "+keyframeprefix+"animation: "+totalTime+"s slidy infinite; }\n"; 
  document.body.appendChild(css);
  }
We hope you would like this Automatic Image Slider using HTML, CSS & JavaScript.

Thank you for reading our blog. If you face any problem in creating this Automatic Image Slider using HTML, CSS & JavaScript, then contact us or comment us. We’ll try to provide a solution to your problem as soon as possible.

Thank you
Learning robo team

Post a Comment

Thank you
Learning robo team

Post a Comment (0)

Previous Post Next Post
Learning Robo says...
code copied
Welcome