我的 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/>   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 新手,所以我不太理解它,所以非常感谢任何帮助!
最佳答案
我想你也这么期待。
<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/
更新了 fiddle https://jsfiddle.net/sg649vy3/4/
如果您希望这些箭头位于中间,请添加此样式
.fa-chevron-left,.fa-chevron-right{
margin-top:180%;
}
关于javascript - 如何使用按钮创建自动幻灯片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42715224/