简体   繁体   English

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

[英]Javascript Slideshow Won't Show First Array Until the Button is Clicked

My slideshow works great except when the page loads initially. 除了页面最初加载时,我的幻灯片效果很好。 I've set my CSS to "display:none" to begin with, then the Javascript loops through the images to display them as inline-blocks when the next button is clicked. 首先,我将CSS设置为“ display:none”,然后单击下一个按钮时,JavaScript循环遍历图像以将它们显示为嵌入式块。 But, the first image displays as "none" to start out. 但是,第一个图像显示为“无”开始。

I'm sure there's a way to just display the first image while keeping all the images after hidden. 我敢肯定,有一种方法可以只显示第一个图像,同时保留所有图像。 I'm pretty new to this. 我对此很陌生。

I followed along a tutorial on w3schools.com here: https://www.w3schools.com/howto/howto_js_slideshow.asp 我在这里跟随w3schools.com上的教程: https : //www.w3schools.com/howto/howto_js_slideshow.asp

You can see my full code and the product on my codepen: https://codepen.io/catherinehh/pen/orpeJV 您可以在我的代码笔上看到我的完整代码和产品: https ://codepen.io/catherinehh/pen/orpeJV

CSS: CSS:

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

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

JAVASCRIPT: 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";
}

I've basically followed the tutorial on w3schools.com line by line, and I can't find where my code differs from the tutorial. 我基本上逐行遵循了w3schools.com上的教程,但找不到与该教程不同的代码。 It should run just like the slideshow on that page. 它应该像该页面上的幻灯片一样运行。

Any help is appreciated! 任何帮助表示赞赏! Thank you! 谢谢!

You have forgotten to initialize the slider by calling showSlides(slideIndex) 您忘记了通过调用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> 

If you want the slider to start working on page load, so you can call the first slide at the end of above codes: 如果您希望滑块开始加载页面,那么您可以在上述代码的末尾调用第一张幻灯片:

showSlides(slideIndex);

For more confident (as I dont know where do you put the script in your page if in Header or after the Body tag) you can use <body onload="showSlides(1)"> to postpone the slider function after complete page load. 为了更自信(因为我不知道如果将脚本放置在Header中还是在Body标记之后,您将该脚本放置在页面中的什么位置),可以使用<body onload="showSlides(1)">在完成页面加载后推迟滑块功能。

If you want to show the first image initially and start the slider on click, so use the CSS rule to exclude the first image of being hidden: 如果要最初显示第一张图像并单击启动滑块,请使用CSS规则排除被隐藏的第一张图像:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM