我的幻灯片一次显示所有图像,直到点击下一个或上一个按钮,然后幻灯片放映功能正常。我找到了一个类似的线程,问题最终是语法错误。我一遍又一遍地阅读代码,我看不出问题。请让我知道我做错了什么。谢谢!
.ssbutton{
border:none;
display:inline-block;
outline:0;
padding:8px 16px;
vertical-align:middle;
overflow:hidden;
text-decoration:none;
color:inherit;
background-color:inherit;
text-align:center;
cursor:pointer;
white-space:nowrap
}
var slideIndex = 1;
showDivs(slideIndex);
function plusDivs(n) {
showDivs(slideIndex += n);
}
function showDivs(n) {
var i;
var x = document.getElementsByClassName("Slide");
if (n > x.length) {slideIndex = 1}
if (n < 1) {slideIndex = x.length}
for (i = 0; i < x.length; i++) {
x[i].style.display = "none";
}
x[slideIndex-1].style.display = "block";
}
<div class="slideShow">
<img class="Slide" src="images/Pic1.png">
<img class="Slide" src="images/Pic2.png">
<img class="Slide" src="images/Pic3.png">
<img class="Slide" src="images/Pic4.png">
<button class="ssbutton" onclick="plusDivs(-1)">❮Prev</button>
<button class="ssbutton" onclick="plusDivs(1)">Next ❯</button>
</div>
答案 0 :(得分:0)
默认情况下,您需要隐藏.Slide
个元素,您可以在CSS中进行设置:
.Slide {
display: none;
}
/* show first slide by default */
.Slide:first-child {
display: block;
}