1

Disclaimer, I'm a total beginner.

I've been working on a website, and I wanted to create an image carousel - it's on a timer to flick through some images, but whenever I click the arrows to scroll through it speeds up? I've tried several things I've found on the web, but it's just not working - yes I am trying to clear my timers but I'm clearly doing something off because it's still not working.

Code:

HTML:


<div class="mySlides fade">
  <div class="numbertext">1 / 5</div>
  <img class="ratio" src="carosel/1.1.jpg" style="width:100%; display:block">
  <div class="title">Cormack Designs</div>
  <div class="caption">kintyre - // beach [Ruari Cormack]</div>
</div>

<div class="mySlides fade">
  <div class="numbertext">2 / 5</div>
  <img class="ratio" src="carosel/2.1.jpg" style="width:100%;">
  <div class="title">Cormack Designs</div>
  <div class="caption">kintyre - // beach [Ruari Cormack]</div>
</div>

<div class="mySlides fade">
  <div class="numbertext">3 / 5</div>
  <img class="ratio" src="carosel/3.1.jpg" style="width:100%;">
  <div class="title">Cormack Designs</div>
  <div class="caption">kintyre - // beach [Ruari Cormack]</div>
</div>

<div class="mySlides fade">
  <div class="numbertext">4 / 5</div>
  <img class="ratio" src="carosel/4.1.jpg" style="width:100%;">
  <div class="title">Cormack Designs</div>
  <div class="caption">kintyre - // beach [Ruari Cormack]</div>
</div>

<div class="mySlides fade">
  <div class="numbertext">5 / 5</div>
  <img class="ratio" src="carosel/5.1.jpg" style="width:100%;">
  <div class="title">Cormack Designs</div>
  <div class="caption">kintyre - // beach [Ruari Cormack]</div>
</div>

<a class="prev" onclick="plusSlides(-1)">&#10094;</a>
<a class="next" onclick="plusSlides(1)">&#10095;</a>

</div>
<br>

<div style="text-align:center">
  <span class="dot" onclick="currentSlide(1)"></span> 
  <span class="dot" onclick="currentSlide(2)"></span> 
  <span class="dot" onclick="currentSlide(3)"></span> 
  <span class="dot" onclick="currentSlide(4)"></span>
  <span class="dot" onclick="currentSlide(5)"></span>
</div>

CSS:

* {box-sizing: border-box}
.mySlides {display: none}
img {vertical-align: middle;}

/* Slideshow container */
.slideshow-container {
  max-width: 100%;
  position: relative;
  margin: auto;
}

.ratio {aspect-ratio: 2/0.80;}

/* 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: 0 3px 3px 0;
  user-select: none;
}

/* Position the "next button" to the right */
.next {
  right: 0;
  border-radius: 3px 0 0 3px;
}

/* 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 */
.caption {
  color: #f2f2f2;
  font-size: 15px;
  padding: 8px 12px;
  position: absolute;
  bottom: 8px;
  width: 100%;
  text-align: center;
}

.title {
    color: #f2f2f2;
    font-size: 40px;
    padding: 20px 20px;
    position: absolute;
    bottom: 130px;
    width: 100%;
    text-align: center;
}

/* Number text (1/3 etc) */
.numbertext {
  color: #f2f2f2;
  font-size: 12px;
  padding: 8px 12px;
  position: absolute;
  top: 0;
}

/* The dots/bullets/indicators */
.dot {
  cursor: pointer;
  height: 15px;
  width: 15px;
  margin: 0 2px;
  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}
}

JavaScript:

var slideIndex = 0; 
showSlides(); // call showslide method 

function plusSlides(n) {
  showSlides(slideIndex += n);
}

var sliderTimer;
function currentSlide(n) {
  console.log(n);
  if (sliderTimer) window.clearTimeout(sliderTimer);
  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";  
  } 

  if(!n) {
    slideIndex++;  
  }

  if (slideIndex > slides.length){ 
      slideIndex = 1; 
  } 

  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"; 

  sliderTimer = setTimeout(showSlides, 7000);  
} 
halfer
  • 19,824
  • 17
  • 99
  • 186
betgan
  • 13
  • 2

1 Answers1

0

For a more consistent animation, its recomended to use requestAnimationFrame instead of setTimeout or setInterval.

For further information, you can refer to:

Why is requestAnimationFrame better than setInterval or setTimeout

requestAnimationFrame loop not correct FPS

https://www.youtube.com/watch?v=cCOL7MC4Pl0

guilfer
  • 164
  • 1
  • 12