Search code examples
javascripthtmlcssslideshow

How to create an automatic slideshow with buttons


I have an automatic slideshow in my HTML that uses JavaScript to change to the next image after a defined time. Here's my code:

<div class="slideshow-container">
<div class="mySlides fade">
  <img alt="Gaming Computer" src="../Pictures/1.jpg" href="../Pages/Systems.html" style="width:100%">
  <div> <h2 class="photoText"><span>Gaming Ready Computers.</span></h2> </div>
</div>

<div class="mySlides fade">
  <img alt="Text" src="../Pictures/2.jpeg" style="width:100%">
  <h2 class="photoText2"><span>The newest, most innovative, and truly powerful GPUs, <br/> &nbsp ready to push your next PC to the next level.</span></h2>
  </div>

<div class="mySlides fade">
  <img alt="Text" src="../Pictures/3.png" style="width:100%">
  <h2 class="photoText3"><span>The Ultimate Gaming Experience.</span></h2>
</div>

<div class="mySlides fade">
  <img alt="Text" src="../Pictures/4.png" style="width:100%">
  <h2 class="photoText4"><span>Industry Quality Editing.</span></h2>
</div>

<div class="mySlides fade">
  <img alt="Text" src="../Pictures/5.jpg" style="width:100%">
  <h2 class="photoText5"><span>Learn to create anything you can imagine.</span></h2>
</div>

<div class="dotContainer" style="text-align:center;">
  <span class="dot"></span> 
  <span class="dot"></span> 
  <span class="dot"></span> 
  <span class="dot"></span>
  <span class="dot"></span> 
</div>

<script>
var slideIndex = 0;
showSlides();

function showSlides() {
    var i;
    var slides = document.getElementsByClassName("mySlides");
    var dots = document.getElementsByClassName("dot");
    for (i = 0; i < slides.length; i++) {
       slides[i].style.display = "none";  
    }
    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";
    setTimeout(showSlides, 6000);
}
</script>

I would like to add two buttons to the right and left of the images that when clicked, change to the next or previous picture. I have been searching for days on end and have not found anything that answered my problem. I have seen one other post on here where someone asked the same question as myself, however the only answer was to use a CSS from a website that was far too long to filter out only the code for the slideshow.

I'm new to JavaScript so I don't understand it really so any help is greatly appreciated!


Solution

  • I think you are expecting like this.

    <a class="left carousel-control" href="#myCarousel" role="button" data-slide="prev">
                                                    <i class="fa fa-chevron-left" aria-hidden="true"></i>
                                                </a>
                                                <a class="right carousel-control" href="#myCarousel" role="button" data-slide="next">
                                                    <i class="fa fa-chevron-right" aria-hidden="true"></i>
                                                </a>
    

    check with your images

    https://jsfiddle.net/sg649vy3/

    updated fiddle https://jsfiddle.net/sg649vy3/4/

    If you want those arrows in middle,add this style

    .fa-chevron-left,.fa-chevron-right{
      margin-top:180%;
    }