Javascript And Css For Automatic Slideshow
Solution 1:
Use setInterval() function to make the slideshow automatically for the certain time interval. Add the setInterval() function in your example reference link after the below line:
var slideIndex = 1; showSlides(slideIndex);
setInterval(function(){ showSlides(++slideIndex); }, 1000);
The slide will be changed every 1000 milli seconds. If you need you can adjust the time as per your need.
Solution 2:
You could do what the click on the next button does: call plusSlider with a parameter 1.
To do that every 2000ms your can use setInterval. On that w3schools tryit page put following code before the closing </script> then run it:
setInterval(plusSlides,2000,1);
Solution 3:
What I would recommend is making use of a setInterval() function such as the following:
setInterval(function() {
slideIndex++;
showSlides(slideIndex);
}, 3000);
The above function increases the current index, then triggers the showSlides() function, passing the index as a parameter. The 3000 denotes the speed at which the automation should occur, with the time specified in millseconds. Note that setInterval() should be used rather than setTimeout(), because setInteravl() fires more than once.
This gives the following result:
var slideIndex = 1;
showSlides(slideIndex);
functionplusSlides(n) {
showSlides(slideIndex += n);
}
functioncurrentSlide(n) {
showSlides(slideIndex = n);
}
functionshowSlides(n) {
var i;
var slides = document.getElementsByClassName("mySlides");
var dots = document.getElementsByClassName("dot");
if (n > slides.length) {
slideIndex = 1
}
if (n < 1) {
slideIndex = slides.length
}
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
for (i = 0; i < dots.length; i++) {
dots[i].className = dots[i].className.replace(" active", "");
}
slides[slideIndex - 1].style.display = "block";
dots[slideIndex - 1].className += " active";
}
setInterval(function() {
slideIndex++;
showSlides(slideIndex);
}, 3000);* {
box-sizing: border-box
}
body {
font-family: Verdana, sans-serif;
margin: 0
}
.mySlides {
display: none
}
/* Slideshow container */.slideshow-container {
max-width: 1000px;
position: relative;
margin: auto;
}
/* Next & previous buttons */.prev,
.next {
cursor: pointer;
position: absolute;
top: 50%;
width: auto;
padding: 16px;
margin-top: -22px;
color: white;
font-weight: bold;
font-size: 18px;
transition: 0.6s ease;
border-radius: 03px3px0;
}
/* Position the "next button" to the right */.next {
right: 0;
border-radius: 3px003px;
}
/* On hover, add a black background color with a little bit see-through */.prev:hover,
.next:hover {
background-color: rgba(0, 0, 0, 0.8);
}
/* Caption text */.text {
color: #f2f2f2;
font-size: 15px;
padding: 8px12px;
position: absolute;
bottom: 8px;
width: 100%;
text-align: center;
}
/* Number text (1/3 etc) */.numbertext {
color: #f2f2f2;
font-size: 12px;
padding: 8px12px;
position: absolute;
top: 0;
}
/* The dots/bullets/indicators */.dot {
cursor: pointer;
height: 13px;
width: 13px;
margin: 02px;
background-color: #bbb;
border-radius: 50%;
display: inline-block;
transition: background-color 0.6s ease;
}
.active,
.dot:hover {
background-color: #717171;
}
/* Fading animation */.fade {
-webkit-animation-name: fade;
-webkit-animation-duration: 1.5s;
animation-name: fade;
animation-duration: 1.5s;
}
@-webkit-keyframes fade {
from {
opacity: .4
}
to {
opacity: 1
}
}
@keyframes fade {
from {
opacity: .4
}
to {
opacity: 1
}
}
/* On smaller screens, decrease text size */@mediaonly screen and (max-width: 300px) {
.prev,
.next,
.text {
font-size: 11px
}
}<!DOCTYPE html><html><head><metaname="viewport"content="width=device-width, initial-scale=1"></head><body><divclass="slideshow-container"><divclass="mySlides fade"><divclass="numbertext">1 / 3</div><imgsrc="https://www.w3schools.com/howto/img_nature_wide.jpg"style="width:100%"><divclass="text">Caption Text</div></div><divclass="mySlides fade"><divclass="numbertext">2 / 3</div><imgsrc="https://www.w3schools.com/howto/img_fjords_wide.jpg"style="width:100%"><divclass="text">Caption Two</div></div><divclass="mySlides fade"><divclass="numbertext">3 / 3</div><imgsrc="https://www.w3schools.com/howto/img_mountains_wide.jpg"style="width:100%"><divclass="text">Caption Three</div></div><aclass="prev"onclick="plusSlides(-1)">❮</a><aclass="next"onclick="plusSlides(1)">❯</a></div><br><divstyle="text-align:center"><spanclass="dot"onclick="currentSlide(1)"></span><spanclass="dot"onclick="currentSlide(2)"></span><spanclass="dot"onclick="currentSlide(3)"></span></div></body></html>Hope this helps! :)
Post a Comment for "Javascript And Css For Automatic Slideshow"