如何使用按钮

时间:2017-03-10 09:49:53

标签: javascript html css slideshow

我的HTML中有一个自动幻灯片,它使用JavaScript在定义的时间后更改为下一个图像。 这是我的代码:

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

我想在图片的左右两侧添加两个按钮,点击后,切换到下一张或上一张图片。我一直在寻找几天没找到任何能解决我问题的东西。我在这里看过另外一篇帖子,其中有人问我自己的问题,但唯一的答案是使用网站上的CSS太长,不能过滤掉幻灯片的代码。

我是JavaScript的新手,所以我真的不明白,所以任何帮助都非常感谢!

1 个答案:

答案 0 :(得分:0)

我认为你期待这样。

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

检查你的照片

https://jsfiddle.net/sg649vy3/

更新小提琴https://jsfiddle.net/sg649vy3/4/

如果你想在中间放置这些箭头,请添加此样式

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