在单击按钮之前,Javascript 幻灯片不会显示第一个数组

标签 javascript html css for-loop slideshow

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

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

我在这里遵循了 w3schools.com 上的教程:https://www.w3schools.com/howto/howto_js_slideshow.asp

您可以在我的代码笔上看到我的完整代码和产品: 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 上的教程进行操作,但我找不到我的代码与教程有何不同。它应该像该页面上的幻灯片一样运行。

感谢任何帮助!谢谢!

最佳答案

您忘记通过调用 showSlides(slideIndex) 来初始化 slider

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>

关于在单击按钮之前,Javascript 幻灯片不会显示第一个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56809496/

相关文章:

javascript - 通过 XMLHttpRequest 从 HTML5 文件系统上传文件

javascript - 如何在发布前减少数字[值]?

html - 框阴影叠加

css - 遇到 z-index 问题并使元素正确堆叠

css - 如何通过 CSS 中的悬停选择器更改 JFXButton 中的 Font Awesome 字形颜色?

javascript - OnePage Scroll 自定义导航

javascript - Jquery animate() 效果不能很好地发挥作用

javascript - 根据另一个选定的下拉列表更改下拉列表中的选项

javascript - 使用 RequireJS 时,我应该将脚本放在 Yeoman 项目的什么位置?

jquery - 表头最大高度,不够横向展开