Java Script幻灯片显示不会显示幻灯片

时间:2017-06-28 14:32:55

标签: javascript html slideshow

我已将此JavaScript幻灯片编码为显示多个图像,但它们不会在任何地方显示,唯一显示的是我选择幻灯片时包含的两个按钮。



var slideIndex = 1;
showDivs(slideIndex);
function plusDivs(n) {
    showDivs(slideIndex += n);
}
function showDivs(n) {
    var i;
    var x= document.getElementsByClassName("slides");
    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"; 
}
&#13;
        <img class="slides" href="Bgimage.jpg">
        <img class="slides" href="roses.jpg">
        <img class="slides" href="sunflowers.jpg">
        <img class="slides" href="Vessel1.jpg">
        <button class="go-left" onclick="plusDivs(-1)">&#10094</button>
        <button class="go-right" onclick="plusDivs(+1)">&#10095</button>
    
&#13;
&#13;
&#13;

非常感谢任何帮助。谢谢!

3 个答案:

答案 0 :(得分:2)

Img代码的src不是href。您的代码完美无缺。

&#13;
&#13;
var slideIndex = 1;
showDivs(slideIndex);
function plusDivs(n) {
    showDivs(slideIndex += n);
}
function showDivs(n) {
    var i;
    var x= document.getElementsByClassName("slides");
    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"; 
}
&#13;
<img class="slides" src="http://fakeimg.pl/250x100/">
        <img class="slides" src="http://fakeimg.pl/250x100/ff0000/">
        <img class="slides" src="http://fakeimg.pl/350x200/ff0000/000">
        <img class="slides" src="http://fakeimg.pl/250x100/ff0000/">
        <button class="go-left" onclick="plusDivs(-1)">&#10094</button>
        <button class="go-right" onclick="plusDivs(+1)">&#10095</button>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

img代码使用 src 来确定图片的来源。如果您将 href 更改为 src ,您应该可以再次看到图片。

<img class="slides" src="Bgimage.jpg">
<img class="slides" src="roses.jpg">
<img class="slides" src="sunflowers.jpg">
<img class="slides" src="Vessel1.jpg">

答案 2 :(得分:0)

var slideIndex = 0;
showDivs(slideIndex);
function plusDivs(n) {
    showDivs(slideIndex += n);
}
function showDivs(n) {
    var i;
    var x= document.getElementsByClassName("slides");
    if (n >= x.length) { slideIndex = 0; }
    if (n < 0) { slideIndex = x.length-1; }

    for (i = 0; i < x.length; i++) {
        if(i!==slideIndex) {
          x[i].style.display = "none";
        } else {
          x[i].style.display = "block";
        }
    }
}
        <img class="slides" src="Bgimage.jpg">
        <img class="slides" src="roses.jpg">
        <img class="slides" src="sunflowers.jpg">
        <img class="slides" src="Vessel1.jpg">
        <button class="go-left" onclick="plusDivs(-1)">&#10094</button>
        <button class="go-right" onclick="plusDivs(+1)">&#10095</button>