javascript - HTML Slider 中的第一张幻灯片在初始页面加载时不显示

标签 javascript html css

所以我有一个从 W3 Schools 获得的 HTML slider 模板,我按照该模板将幻灯片放映到我公司使用原始 HTML 管理的 Facebook 页面上。我的问题是,首次加载页面时,第一张幻灯片根本不显示。直到用户继续到下一张幻灯片时才会加载图像。 我已经将我的代码与原始代码进行了比较,应该没有错,因为我实际上只是使用了模板并更改了一些文本/按钮颜色。

这是我遵循的模板:https://www.w3schools.com/howto/howto_js_slideshow.asp

这是代码:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Woobox Digital Deals Slider</title>
</head>

<body>

<script>
    var slideIndex = 1;
showSlides(slideIndex);

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

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

function showSlides(n) {
  var i;
  var slides = document.getElementsByClassName("mySlides");
  var dots = document.getElementsByClassName("dot");
  if (n > slides.length) {slideIndex = 1} 
  if (n < 1) {slideIndex = slides.length}
  for (i = 0; i < slides.length; i++) {
      slides[i].style.display = "none"; 
  }
  for (i = 0; i < dots.length; i++) {
      dots[i].className = dots[i].className.replace(" active", "");
  }
  slides[slideIndex-1].style.display = "block"; 
  dots[slideIndex-1].className += " active";
}

</script>

<style>
* {box-sizing:border-box}

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

.mySlides {
    display: none;
}

/* Next & previous buttons */
.prev, .next {
    background-color: rgba(0,0,0,0.65);
  cursor: pointer;
  position: absolute;
  top: 50%;
  width: auto;
  margin-top: -22px;
  padding: 16px;
  color: white;
  font-weight: bold;
  font-size: 18px;
  transition: 0.6s ease;
  border-radius: 0 3px 3px 0;
}

/* Position the "next button" to the right */
.next {
  right: 0;
  border-radius: 3px 0 0 3px;
}

/* On hover, add a black background color with a little bit see-through */
.prev:hover, .next:hover {
  background-color: rgba(0,0,0,0.8);
}

/* Heading Text */
#slideshow-heading {
    color: rgba(222,42,0,1.00); /*Primary site color goes here*/
    font-size: 18px;
    text-align: center;
    padding: 20px 10px 20px 10px
}

/*Comment styling*/ 
.text {
        visibility: hidden;
    }

/* Number text (1/3 etc) */
.numbertext {
  color: #000000;
  font-size: 12px;
  padding: 8px 12px;
  position: absolute;
  top: 0;
}

/* The dots/bullets/indicators */
.dot {
  cursor:pointer;
  height: 13px;
  width: 13px;
  margin: 0 2px;
  background-color: #bbb;
  border-radius: 50%;
  display: inline-block;
  transition: background-color 0.6s ease;
}

.active, .dot:hover {
  background-color: #717171;
}

/* Fading animation */
.fade {
  -webkit-animation-name: fade;
  -webkit-animation-duration: 1.5s;
  animation-name: fade;
  animation-duration: 1.5s;
}

@-webkit-keyframes fade {
  from {opacity: .4} 
  to {opacity: 1}
}

@keyframes fade {
  from {opacity: .4} 
  to {opacity: 1}
}
</style>


<div>
    <p id="slideshow-heading">
        Click or Tap image to print
    </p>
</div>
 <div class="slideshow-container">
  <div class="mySlides fade">
    <a href="google.com" target="_blank">
        <img src="http://www.shoppingindanville.com/wp-content/uploads/2017/06/Artboard-19.jpg" style="width:100%;height:auto;">
    </a>
  </div>

  <div class="mySlides fade">
    <img src="http://www.shoppingindanville.com/wp-content/uploads/2017/06/Artboard-18.jpg" style="width:100%;height:auto;">
  </div>

  <div class="mySlides fade">
    <img src="http://www.shoppingindanville.com/wp-content/uploads/2017/06/Artboard-17.jpg" style="width:100%;height:auto;">
  </div>

  <a class="prev" onclick="plusSlides(-1)">&#10094;</a>
  <a class="next" onclick="plusSlides(1)">&#10095;</a>
</div>




</body>
</html>


感谢@Zerone 向我解释了为什么我的代码不起作用。我没有意识到当页面被加载时,它执行的代码是顺序的。这有助于加深我对 HTML 行为方式的理解,再次感谢 Zerone!

最佳答案

如果您将脚本 block 向下移动到 html 的底部,在最后一个 div 之后和 body 标记之前,如下所示:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Woobox Digital Deals Slider</title>
</head>

<body>



<style>
* {box-sizing:border-box}

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

.mySlides {
    display: none;
}

/* Next & previous buttons */
.prev, .next {
    background-color: rgba(0,0,0,0.65);
  cursor: pointer;
  position: absolute;
  top: 50%;
  width: auto;
  margin-top: -22px;
  padding: 16px;
  color: white;
  font-weight: bold;
  font-size: 18px;
  transition: 0.6s ease;
  border-radius: 0 3px 3px 0;
}

/* Position the "next button" to the right */
.next {
  right: 0;
  border-radius: 3px 0 0 3px;
}

/* On hover, add a black background color with a little bit see-through */
.prev:hover, .next:hover {
  background-color: rgba(0,0,0,0.8);
}

/* Heading Text */
#slideshow-heading {
    color: rgba(222,42,0,1.00); /*Primary site color goes here*/
    font-size: 18px;
    text-align: center;
    padding: 20px 10px 20px 10px
}

/*Comment styling*/ 
.text {
        visibility: hidden;
    }

/* Number text (1/3 etc) */
.numbertext {
  color: #000000;
  font-size: 12px;
  padding: 8px 12px;
  position: absolute;
  top: 0;
}

/* The dots/bullets/indicators */
.dot {
  cursor:pointer;
  height: 13px;
  width: 13px;
  margin: 0 2px;
  background-color: #bbb;
  border-radius: 50%;
  display: inline-block;
  transition: background-color 0.6s ease;
}

.active, .dot:hover {
  background-color: #717171;
}

/* Fading animation */
.fade {
  -webkit-animation-name: fade;
  -webkit-animation-duration: 1.5s;
  animation-name: fade;
  animation-duration: 1.5s;
}

@-webkit-keyframes fade {
  from {opacity: .4} 
  to {opacity: 1}
}

@keyframes fade {
  from {opacity: .4} 
  to {opacity: 1}
}
</style>


<div>
    <p id="slideshow-heading">
        Click or Tap image to print
    </p>
</div>
 <div class="slideshow-container">
  <div class="mySlides fade">
    <a href="google.com" target="_blank">
        <img src="http://www.shoppingindanville.com/wp-content/uploads/2017/06/Artboard-19.jpg" style="width:100%;height:auto;">
    </a>
  </div>

  <div class="mySlides fade">
    <img src="http://www.shoppingindanville.com/wp-content/uploads/2017/06/Artboard-18.jpg" style="width:100%;height:auto;">
  </div>

  <div class="mySlides fade">
    <img src="http://www.shoppingindanville.com/wp-content/uploads/2017/06/Artboard-17.jpg" style="width:100%;height:auto;">
  </div>

  <a class="prev" onclick="plusSlides(-1)">&#10094;</a>
  <a class="next" onclick="plusSlides(1)">&#10095;</a>
</div>

<script>
    var slideIndex = 1;
showSlides(slideIndex);

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

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

function showSlides(n) {
  var i;
  var slides = document.getElementsByClassName("mySlides");
  var dots = document.getElementsByClassName("dot");
  if (n > slides.length) {slideIndex = 1} 
  if (n < 1) {slideIndex = slides.length}
  for (i = 0; i < slides.length; i++) {
      slides[i].style.display = "none"; 
  }
  for (i = 0; i < dots.length; i++) {
      dots[i].className = dots[i].className.replace(" active", "");
  }
  slides[slideIndex-1].style.display = "block"; 
  dots[slideIndex-1].className += " active";
}

</script>


</body>
</html>

这会停止 Java 的执行,直到页面内容的其余部分加载完毕 :)

关于javascript - HTML Slider 中的第一张幻灯片在初始页面加载时不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46125212/

相关文章:

javascript - 可手动删除设置[特别是宽度和高度]

javascript - 如何通过javascript一次更改多个网页的颜色?

html - CSS - 选择标签的子元素直到下一个相同类型的标签

javascript - Bootstrap 导航栏是否可以像 Bootstrap 选项卡一样工作?

html - 相邻 flexbox 容器中内容的垂直对齐

javascript - mysql数据库填充表中的多个搜索选项

javascript - AngularJs 绑定(bind)选择

javascript - 如何使用 Angular 查找动态添加的类名

javascript - 使用 instafeed.js - 空白页面,不起作用

html - div 和 ul 之间奇怪的垂直空间