如何暂停鼠标悬停幻灯片

时间:2017-03-02 21:07:59

标签: javascript html css slideshow

我正试图让以下幻灯片显示暂停鼠标悬停。现在,当您将鼠标悬停在图像上时,幻灯片会在1和1之间反弹。 3或2& 4似乎是时间的一半。

  var slideIndex = 1;
  var millis = 1000;

  nextSlide();
  var interval = setInterval(nextSlide, millis);

  function resumeSlides() {
    nextSlide();
  }

  function pauseSlides() {
    clearInterval(interval);
  }

  function nextSlide() {
    showSlide();
    slideIndex++;
  }

  function plusSlides(n) {
    clearInterval(interval);
    slideIndex += n;
    nextSlide();
    interval = setInterval(nextSlide, millis);
  }

  function currentSlide(n) {
    clearInterval(interval);
    slideIndex = n + 1;
    nextSlide();
    interval = setInterval(nextSlide, millis);
  }

  function showSlide() {
    var i;
    var slides = document.getElementsByClassName("mySlides");
    var dots = document.getElementsByClassName("dot");
    for (i = 0; i < slides.length; i++) {
      slides[i].style.display = "none";
    }
    if (slideIndex > slides.length) {
      slideIndex = 1;
    }
    if (slideIndex < 1) {
      slideIndex = slides.length;
    }
    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";
  }
/* Slideshow container */
.slideshow-container {
  width: 600px;
  position: relative;
  margin: auto;
  height: 600px;
}

.slideshow-nav {
  text-align:center; 
  padding: 30px 0;
}
  
.mySlides {
  width: auto;
}
  
/* The dots/bullets/indicators */
.dot {
  height: 13px;
  width: 13px;
  margin: 0 2px;
  background-color: lightgray;
  border-radius: 50%;
  display: inline-block;
  transition: background-color 0.6s ease;
}

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

/* Next & previous buttons */
.prev, .next {
  cursor: pointer;
  position: absolute;
  top: 20px;
  width: auto;
  padding: 6px;
  color: black;
  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;
}

.prev:hover, .next:hover {
  background-color: black;
  color: white;
}
<div class="slideshow-container">
  <a class="prev" onclick="plusSlides(-2)">&#10094;</a>
  <div class="slideshow-nav">
    <span class="dot" onclick="currentSlide(0)"></span>
    <span class="dot" onclick="currentSlide(1)"></span>
    <span class="dot" onclick="currentSlide(2)"></span>
    <span class="dot" onclick="currentSlide(3)"></span>
  </div>
  <a class="next" onclick="plusSlides(0)">&#10095;</a>

  <div onmouseover="pauseSlides()" onmouseout="resumeSlides()">
    <!--Slide 1-->
    <div class="mySlides">
      <p>Image 1</p>
      <img src="//dummyimage.com/600">
    </div> 

    <!--Slide 2-->
    <div class="mySlides">
      <p>Image 2</p>
      <img src="//dummyimage.com/600">
    </div>

    <!--Slide 3-->
    <div class="mySlides">
      <p>Image 3</p>
      <img src="//dummyimage.com/600">
    </div>

    <!--Silde 4-->
    <div class="mySlides">
      <p>Image 4</p>
      <img src="//dummyimage.com/600">
    </div>
  </div>
</div>

现在它在片段中我看到有错误。 “message”:“ReferenceError:未定义puaseSlides”, 我不确定这意味着什么或如何解决它。

1 个答案:

答案 0 :(得分:2)

  1. 修正您的拼写错误puaseSlides()
  2. 将初始化代码包装到函数中(请参阅startSlides()
  3. 使用mouseenter代替mouseovermouseleave代替mouseout
  4. var slideIndex = 1;
      var millis = 1000;
      var interval;
      
      startSlides();
      
      function startSlides(){
        pauseSlides();
        nextSlide();
        interval = setInterval(nextSlide, millis);
      }
    
    
      function resumeSlides() {
        nextSlide();
      }
    
      function pauseSlides() {
        clearInterval(interval);
      }
    
      function nextSlide() {
        showSlide();
        slideIndex++;
      }
    
      function plusSlides(n) {
        clearInterval(interval);
        slideIndex += n;
        nextSlide();
        interval = setInterval(nextSlide, millis);
      }
    
      function currentSlide(n) {
        clearInterval(interval);
        slideIndex = n + 1;
        nextSlide();
        interval = setInterval(nextSlide, millis);
      }
    
      function showSlide() {
        var i;
        var slides = document.getElementsByClassName("mySlides");
        var dots = document.getElementsByClassName("dot");
        for (i = 0; i < slides.length; i++) {
          slides[i].style.display = "none";
        }
        if (slideIndex > slides.length) {
          slideIndex = 1;
        }
        if (slideIndex < 1) {
          slideIndex = slides.length;
        }
        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";
      }
    /* Slideshow container */
    .slideshow-container {
      width: 600px;
      position: relative;
      margin: auto;
      height: 600px;
    }
    
    .slideshow-nav {
      text-align:center; 
      padding: 30px 0;
    }
      
    .mySlides {
      width: auto;
    }
      
    /* The dots/bullets/indicators */
    .dot {
      height: 13px;
      width: 13px;
      margin: 0 2px;
      background-color: lightgray;
      border-radius: 50%;
      display: inline-block;
      transition: background-color 0.6s ease;
    }
    
    .active, .dot:hover {
      background-color: #717171;
    }
    
    /* Next & previous buttons */
    .prev, .next {
      cursor: pointer;
      position: absolute;
      top: 20px;
      width: auto;
      padding: 6px;
      color: black;
      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;
    }
    
    .prev:hover, .next:hover {
      background-color: black;
      color: white;
    }
    <div class="slideshow-container">
      <a class="prev" onclick="plusSlides(-2)">&#10094;</a>
      <div class="slideshow-nav">
        <span class="dot" onclick="currentSlide(0)"></span>
        <span class="dot" onclick="currentSlide(1)"></span>
        <span class="dot" onclick="currentSlide(2)"></span>
        <span class="dot" onclick="currentSlide(3)"></span>
      </div>
      <a class="next" onclick="plusSlides(0)">&#10095;</a>
    
      <div onmouseenter="pauseSlides()" onmouseleave="startSlides()">
        <!--Slide 1-->
        <div class="mySlides">
          <p>Image 1</p>
          <img src="//dummyimage.com/600">
        </div> 
    
        <!--Slide 2-->
        <div class="mySlides">
          <p>Image 2</p>
          <img src="//dummyimage.com/600">
        </div>
    
        <!--Slide 3-->
        <div class="mySlides">
          <p>Image 3</p>
          <img src="//dummyimage.com/600">
        </div>
    
        <!--Silde 4-->
        <div class="mySlides">
          <p>Image 4</p>
          <img src="//dummyimage.com/600">
        </div>
      </div>
    </div>