Javascript图片幻灯片显示在页面加载时未选择初始图片

时间:2016-07-30 03:37:54

标签: javascript

我将在前言中说我是一个javascript新手。

问题:我有一个使用javascript的5张图片幻灯片,在加载网页时,会在页面下方显示彼此之下的图片,而不是相互重叠。

一旦我选择“向左”或“向右”切换箭头移动到下一个图像,图像就会正确地相互重叠并显示正确的图像。在我刷新页面之前,幻灯片正常工作。

HTML,CSS和Java如下:

HTML:

<div class="image_slider">
    <div class="slideshow-container">
      <div class="mySlides fade">
          <img src="slider_3.png" style="width:100%">
      </div>

      <div class="mySlides fade">
          <img src="slider_2.png" style="width:100%">
      </div>

      <div class="mySlides fade">
        <img src="slider_1.png" style="width:100%">
      </div>

      <div class="mySlides fade">
        <img src="slider_4.png" style="width:100%">
      </div>

      <div class="mySlides fade">
        <img src="slider_5.png" style="width:100%">
      </div>

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

    <br>

    <div style="text-align:center">
      <span class="dot" onclick="currentSlide(1)"></span> 
      <span class="dot" onclick="currentSlide(2)"></span> 
      <span class="dot" onclick="currentSlide(3)"></span> 
      <span class="dot" onclick="currentSlide(4)"></span> 
      <span class="dot" onclick="currentSlide(5)"></span> 
    </div>
  </div>

CSS:

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

    /* Next & previous buttons */
    .prev, .next {
      cursor: pointer;
      position: absolute;
      top: 0;
      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(254,213,221,0.6);
    }

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

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

    /* 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}
    }

......和Java:

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

window.onload = function currentSlide(n) {
  showSlides(slideIndex = n);
}

window.onload = 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";

  var slideIndex = 1;
  showSlides(slideIndex);
}

感谢您的帮助。如果我可以提供更多信息,我可以使用。

1 个答案:

答案 0 :(得分:0)

编辑:onload函数的语法错误。我刚刚更新了答案。

乍一看,似乎你正在调用一个尚不存在的函数。只需移动showSlides(slideIndex);到文件的末尾。记住JS几乎同时执行,一个好的做法是将你的函数放在JS文件中:)

如果这没有用,请告诉我们。

编辑:(没意识到你可以编辑答案哈哈)

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";

}

var slideIndex = 1;

window.onload = function(e){ 
  showSlides(slideIndex);
};