Being quite new to Javascript, I cannot solve this issue on my own.
I have this simple slider with "next" and "previous" -buttons, and dots which indicate which slide is currently showing. The buttons work properly, and the slider also has setInterval -funtion which shows each slide after 2 seconds.
My only problem is, that I would like the setInterval -function to stop when user clicks either the "next" or "previous" -button or one of the dots. Could someone kindly help me to achieve this? Thank you in advance!
var slideIndex = 1;
showSlides(slideIndex);
function plusSlides(n) {
showSlides(slideIndex += n);
}
window.setInterval(function(){
plusSlides(1);
}, 2000);
function currentSlide(n) {
showSlides(slideIndex = n);
}
function showSlides(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";
}
.logocontainer {
width: 800px;
height: 100px;
display: flex;
justify-content: space-around;
align-items: center;
background-color: lightblue;
}
.maincontainer {
width: 800px;
height: 150px;
display: flex;
justify-content: center;
align-items: center;
background-color: lightblue;
}
.dot-container {
width: 800px;
height: 50px;
display: flex;
justify-content: center;
align-items: center;
background-color: lightblue;
}
.dot {
cursor: pointer;
height: 15px;
width: 15px;
background-color: #fff;
border-radius: 50%;
margin: 5px;
}
.active, .dot:hover {
background-color: black;
}
.ctacontainer {
width: 800px;
height: 100px;
display: flex;
justify-content: center;
align-items: center;
background-color: lightblue;
}
.ctacontainer a {
text-decoration: none;
background-color: orange;
padding: 10px 20px 10px 20px;
border-radius: 3px;
color: white;
}
.containeritems {
width: 400px;
height: 150px;
display: flex;
position: relative;
justify-content: center;
align-items: center;
}
.mySlides div:nth-child(n) {
animation-name: fader;
animation-duration: 1s;
z-index: 20;
}
@keyframes fader {
from { opacity: 0.0; }
to { opacity: 1.0; }
}
.prev, .next {
cursor: pointer;
padding: 12px;
user-select: none;
background-color: #000;
color: white;
margin: 5px;
border-radius: 3px;
}
.image1, .image2, .image3 {
display: flex;
justify-content: center;
align-items: center; /* OR FLEX-END */
position: relative;
color: white;
width: 700px;
height: 150px;
}
.image1 {
background-color: lightgreen;
}
.image2 {
background-color: lightgrey;
}
.image3 {
background-color: red;
}
<head>
<link rel = "stylesheet"
type = "text/css"
href = "Simpleslider.css" />
<meta name="viewport" content="width=800, initial-scale=1">
</head>
<div class="logocontainer">
<h1>Logo</h1>
</div>
<div class="maincontainer">
<div class="containeritems">
<a class="prev" onclick="plusSlides(-1)">❮</a>
<div class="mySlides">
<div class="image1"><h2>Image 1</h2></div>
</div>
<div class="mySlides">
<div class="image2"><h2>Image 2</h2></div>
</div>
<div class="mySlides">
<div class="image3"><h2>Image 3</h2></div>
</div>
<a class="next" onclick="plusSlides(1)">❯</a>
</div>
</div>
<div class="dot-container">
<span class="dot" onclick="currentSlide(1)"></span>
<span class="dot" onclick="currentSlide(2)"></span>
<span class="dot" onclick="currentSlide(3)"></span>
</div>
<div class="ctacontainer">
<a href="#">Click here</a>
</div>
<!-- INCLUDE JAVASCRIPT-->
<script type="text/javascript" src="Simpleslider.js"></script>