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.
*{
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;
}
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);
}
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.
Post a Comment
Thank you
Learning robo team