代码之家  ›  专栏  ›  技术社区  ›  P4PL4

Java脚本幻灯片放映不会显示幻灯片

  •  -2
  • P4PL4  · 技术社区  · 7 年前

    我编写了这个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"; 
    }
            <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>
        

    非常感谢您的帮助。谢谢

    3 回复  |  直到 7 年前
        1
  •  2
  •   Dinesh undefined    7 年前

    Img 标签有 src href 。您的代码运行良好。

    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"; 
    }
    <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>
        2
  •  1
  •   hRdCoder    7 年前

    img 标签使用 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">
    
        3
  •  0
  •   Yordan Nikolov    7 年前

    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>