我不知道为什么我的滑块按钮不起作用而且它们也没有显示所有图像....有些人请帮忙
<div class="slides">
<img src="../page_images/1.jpeg" width="100%" height="100%"/>
</div>
<div class="slides">
<img src="../page_images/2.jpeg" width="100%" height="100%"/>
</div>
<div class="slides">
<img src="../page_images/3.jpeg" width="100%" height="100%"/>
</div>
<div class="slides">
<img src="../page_images/4.jpeg" width="100%" height="100%"/>
</div>
&安培;#10094 &安培;#10095
和java脚本
var index = 1;
function plusIndex(n){
index = index + n;
showImage(index);
}
showImage(index);
function showImage(n){
var i;
var x = document.getElementsByClassName("slides");
if(n > x.length){
index = 1;
}
if(n < x.length){
index = x.length;
}
for(i=0; i<x.length;i++){
x[i].style.display = "none";
}
x[index-1].style.display = "block";
}
答案 0 :(得分:2)
此处的解决方案 -
您只需要更改n < x.length
条件
var index = 1;
function plusIndex(n) {
index = index + n;
showImage(index);
}
showImage(index);
function showImage(n) {
var i;
var x = document.getElementsByClassName("slides");
if (n > x.length) {
index = 1;
}
if (n <= 0) {
index = x.length;
}
for (i = 0; i < x.length; i++) {
x[i].style.display = "none";
}
x[index - 1].style.display = "block";
}
&#13;
<div class="marbletypeleft">
<div class="slides">
<img src="../page_images/1.jpeg" alt="1" width="100%" height="100%" />
</div>
<div class="slides">
<img src="../page_images/2.jpeg" alt="2" width="100%" height="100%" />
</div>
<div class="slides">
<img src="../page_images/3.jpeg" alt="3" width="100%" height="100%" />
</div>
<div class="slides">
<img src="../page_images/4.jpeg" alt="4" width="100%" height="100%" />
</div>
<button class="btn" onclick="plusIndex(-1)" id="btn1">❮</button>
<button class="btn" onclick="plusIndex(1)" id="btn2">❯</button>
</div>
&#13;