【问题标题】:Javascript Slideshow Won't Show First Array Until the Button is Clicked在单击按钮之前,Javascript 幻灯片不会显示第一个数组
【发布时间】:2019-06-28 15:23:36
【问题描述】:

除了页面最初加载时,我的幻灯片效果很好。我一开始就将我的 CSS 设置为“display:none”,然后当点击下一个按钮时,Javascript 循环遍历图像以将它们显示为内联块。但是,开始时第一张图片显示为“无”。

我确信有一种方法可以只显示第一张图片,同时隐藏所有图片。我对此很陌生。

我在 w3schools.com 上学习了一个教程:https://www.w3schools.com/howto/howto_js_slideshow.asp

您可以在我的 codepen 上查看我的完整代码和产品: https://codepen.io/catherinehh/pen/orpeJV

CSS:

.slideshow-container {
  max-width: 1000px;
  position: relative;
  margin: auto;
}

/* Hide the images by default */
.slideImg {
 display: none;
}

JAVASCRIPT:

var slideIndex = 1;

function nextSlide(n){
 showSlides(slideIndex += n);
}

function currentSlide(n){
 showSlides(slideIndex = n);
}

function showSlides(n){
  var i;
  var slides = document.getElementsByClassName("slideImg");
  if (n > slides.length){
   slideIndex = 1;
  }
  if (n < 1){
   slideIndex = slides.length;
  }
  for (i = 0; i < slides.length; i++) {
   slides[i].style.display = "none";
  }
   slides[slideIndex-1].style.display = "inline-block";
}

我基本上是逐行遵循 w3schools.com 上的教程,但我找不到我的代码与教程的不同之处。它应该像该页面上的幻灯片一样运行。

感谢任何帮助!谢谢!

【问题讨论】:

  • 您必须在&lt;body&gt; 的底部添加一个&lt;script&gt;

标签: javascript html css for-loop slideshow


【解决方案1】:

你忘记调用showSlides(slideIndex)来初始化滑块

var slideIndex = 1;

function nextSlide(n){
  showSlides(slideIndex += n);
}

function currentSlide(n){
  showSlides(slideIndex = n);
}

function showSlides(n){
  var i;
  var slides = document.getElementsByClassName("slideImg");
  if (n > slides.length){
    slideIndex = 1;
  }
  if (n < 1){
    slideIndex = slides.length;
  }
  for (i = 0; i < slides.length; i++) {
    slides[i].style.display = "none";
  }
  slides[slideIndex-1].style.display = "inline-block";
}
showSlides(slideIndex)
.slideshow-container {
  max-width: 1000px;
  position: relative;
  margin: auto;
}

/* Hide the images by default */
.slideImg {
  display: none;
}
<div class="wrapper">


  <header>

    <h1>Catherine Hill Hyman</h1>
    <p>Professional Creations</p>

  </header>


  <main>

    <section class="designSection">
      <h2>Design</h2>
    </section>


    <section class="developmentSection">
      <h2>Web Development</h2>
    </section>


    <section class="photoSection">
      <h2>Photography</h2>

      <!-- begin photo slideshow-->
      <div class="photoSlideshow">

        <button name="prevButt" onclick="nextSlide(-1)">&#10094;</button>

        <div class="slideImg">
          <img id="uncleBud" src="https://scontent.fcae1-1.fna.fbcdn.net/v/t1.0-9/58378586_10157043937729774_1705977854832934912_n.jpg?_nc_cat=111&_nc_oc=AQl-LK_lpsqPa3rT2zTbc5KJpNNZECz9jxm15Xk0bNbYjtTklfxf34uVybG2xSjjffA&_nc_ht=scontent.fcae1-1.fna&oh=c5b64ef84d76c6d7ef2a9b9409e12637&oe=5DBFF07F"
            alt="black and white image of half a woman's face with dark hair looming over the camera. Her eyes are not visible." style="height:300px">
        </div>

        <div class="slideImg">
          <img src="https://scontent.fcae1-1.fna.fbcdn.net/v/t1.0-9/61008492_10157093314404774_1267282211523002368_o.jpg?_nc_cat=111&_nc_oc=AQldqecidxJd5LhQsCrYpnGnQ3mmLDqqMe9OGcChpmZyP82vSw7oBZvBDCZfbYktIGQ&_nc_ht=scontent.fcae1-1.fna&oh=c57a3d3827f6cab302470ec0e697f7ed&oe=5D88E446"
            alt="black and white image of half a woman's face with dark hair looming over the camera. Her eyes are not visible." style="height:300px">
        </div>

        <div class="slideImg">
          <img src="https://scontent.fcae1-1.fna.fbcdn.net/v/t1.0-9/59285770_10157043934564774_1801122100677705728_n.jpg?_nc_cat=107&_nc_oc=AQlfwDfzSUayQL0nmF9apZnwLzQJtIZw3KsWx2mM5P0O1x2q508-vo4lsCD6EfUD9ik&_nc_ht=scontent.fcae1-1.fna&oh=4948f8ea74480440b542e60d119e23a9&oe=5D7E8C45"
            alt="black and white image of half a woman's face with dark hair looming over the camera. Her eyes are not visible." style="height:300px">
        </div>

        <button name="nextButt" onclick="nextSlide(1)">&#10095;</button>

        <script src="slideShow.js"></script>

      </div>

      <!-- end photo slideshow-->

    </section>


    <section class="writingSection">
      <h2>Writing</h2>
    </section>

  </main>

【讨论】:

    【解决方案2】:

    如果您希望滑块在页面加载时开始工作,那么您可以在上述代码末尾调用第一张幻灯片:

    showSlides(slideIndex);
    

    为了更加自信(因为我不知道如果在 Header 或 Body 标记之后,您将脚本放在页面的哪个位置),您可以使用 &lt;body onload="showSlides(1)"&gt; 在页面加载完成后推迟滑块功能。

    如果您想先显示第一张图片并在点击时启动滑块,请使用 CSS 规则排除第一张被隐藏的图片:

    .slideImg:first-of-type {
     display: block;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多