如果单击按钮,则向前或向后旋转传送带,否则定时向前旋转

时间:2019-01-21 20:01:11

标签: javascript html css

我正在尝试创建手动和自动滚动幻灯片。当单击左向箭头时,轮播无法前进到下一张幻灯片,而单击右向箭头时,我无法在幻灯片中向后移动,同时保持5秒钟自动前进到下一页的麻烦。

window.onload = function() { // add window.onload here and set it euqal to a function
		var slideIndex = 0;
		showSlides(slideIndex);
		
		// Next/previous controls
		function plusSlides(n) {
		  showSlides(slideIndex += n);
		}
		// Thumbnail image controls
		function currentSlide(n) {
		  showSlides(slideIndex = n);
		}

		function showSlides(n) {
		  var i;
		  var slides = document.getElementsByClassName("mySlides");
		  var dots = document.getElementsByClassName("dot");
		  
		  for (i = 0; i < slides.length; i++) {
			slides[i].style.display = "none";  
		  }
		  slideIndex++;
		  if (slideIndex > slides.length) 
			{
			  slideIndex = 1
			}   
		  if (n > slides.length) {
			slideIndex = 0
			} 
		  
		  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";
		  setTimeout(showSlides, 5000); // Change image every 5 seconds
		}
		
		
	}
	
* {box-sizing:border-box}

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

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

/* Next & previous buttons */
.prev, .next {
  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;
  user-select: none;
}

/* 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);
}
*/
/* Caption text */
.text {
  color: #f2f2f2;
  
  font-size: 28px;
  padding: 8px 12px;
  position: absolute;
  bottom: 8px;
  width: 100%;
  text-align: center;
}

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

/* The dots/bullets/indicators */
.dot {
  cursor: pointer;
  height: 15px;
  width: 15px;
  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}
}
<html>

		<!-- Slideshow container -->
		<div class="slideshow-container">

		  <!-- Full-width images with number and caption text -->
		  <div class="mySlides fade">
			<img src="https://cdn.discordapp.com/attachments/502541850985496596/536997100823773184/image0.jpg" style="width:970px; height: 500px;">
			<div class="text"><span style = "background-color: black" >one on one weekly siminars</span></div>
		  </div>

		  <div class="mySlides fade">
			<img src="https://cdn.discordapp.com/attachments/502541850985496596/536997228791988244/image0.jpg" style="width:970px; height: 500px;">
			<div class="text"><span style = "background-color: black" >monthly seminars/workshops</span></div>
		  </div>

		  <div class="mySlides fade">
			<img src="https://cdn.discordapp.com/attachments/502541850985496596/536997338716176395/image0.jpg" style="width:970px; height: 500px;">
			<div class="text"><span style = "background-color: black" >nursing home visits</span></div>
		  </div>
		  
		  <div class="mySlides fade">
			<img src="pictures%20updated/42.jpg" style="width:970px; height: 500px;">
			<div class="text"><span style = "background-color: black" >community service</span></div>
		  </div>
		  
		  <div class="mySlides fade">
			<img src="pictures%20updated/21.jpg" style="width:970px; height: 500px;">
			<div class="text"><span style = "background-color: black" >college visits</span></div>
		  </div>

		  <!-- Next and previous buttons -->
		  <a class="prev" onclick="plusSlides(-1)">&#10094;</a>
		  <a class="next" onclick="plusSlides(1)">&#10095;</a>
		</div>
		<br>
		
		

		<!-- The dots/circles -->
		<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="curretnslide(4)"></span>
		  <span class="dot" onclick="currentslide(5)"></span>
		</div>
	</body>

2 个答案:

答案 0 :(得分:1)

它仍然有点越野车,但这是您想要的吗?

var slideIndex = 0;
showSlides(slideIndex);

// Next/previous controls
function plusSlides(n) {
  showSlides((slideIndex += n));
}
// Thumbnail image controls
function currentSlide(n) {
  showSlides((slideIndex = n));
}

function showSlides(n) {
  var i;
  var slides = document.getElementsByClassName("mySlides");
  var dots = document.getElementsByClassName("dot");

  for (i = 0; i < slides.length; i++) {
    slides[i].style.display = "none";
  }
  slideIndex++;
  if (slideIndex > slides.length) {
    slideIndex = 1;
  }
  if (n > slides.length) {
    slideIndex = 0;
  }

  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";
  setTimeout(showSlides, 5000); // Change image every 5 seconds
}

    		
* {box-sizing:border-box}

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

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

/* Next & previous buttons */
.prev, .next {
  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;
  user-select: none;
}

/* 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);
}
*/
/* Caption text */
.text {
  color: #f2f2f2;
  
  font-size: 28px;
  padding: 8px 12px;
  position: absolute;
  bottom: 8px;
  width: 100%;
  text-align: center;
}

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

/* The dots/bullets/indicators */
.dot {
  cursor: pointer;
  height: 15px;
  width: 15px;
  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}
}
<html>

  <head>

  </head>

  <body>
    <!-- Slideshow container -->
    <div class="slideshow-container">

      <!-- Full-width images with number and caption text -->
      <div class="mySlides fade">
        <img src="https://cdn.discordapp.com/attachments/502541850985496596/536997100823773184/image0.jpg" style="width:970px; height: 500px;">
        <div class="text"><span style="background-color: black">one on one weekly siminars</span></div>
      </div>

      <div class="mySlides fade">
        <img src="https://cdn.discordapp.com/attachments/502541850985496596/536997228791988244/image0.jpg" style="width:970px; height: 500px;">
        <div class="text"><span style="background-color: black">monthly seminars/workshops</span></div>
      </div>

      <div class="mySlides fade">
        <img src="https://cdn.discordapp.com/attachments/502541850985496596/536997338716176395/image0.jpg" style="width:970px; height: 500px;">
        <div class="text"><span style="background-color: black">nursing home visits</span></div>
      </div>

      <div class="mySlides fade">
        <img src="pictures%20updated/42.jpg" style="width:970px; height: 500px;">
        <div class="text"><span style="background-color: black">community service</span></div>
      </div>

      <div class="mySlides fade">
        <img src="pictures%20updated/21.jpg" style="width:970px; height: 500px;">
        <div class="text"><span style="background-color: black">college visits</span></div>
      </div>

      <!-- Next and previous buttons -->
      <a class="prev" onclick="plusSlides(1)">&#10094;</a>
      <a class="next" onclick="plusSlides(1)">&#10095;</a>
    </div>
    <br>

    <!-- The dots/circles -->
    <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>
  </body>

</html>

答案 1 :(得分:0)

在这里,我解决了您的问题。添加了功能bind,它将重置时间间隔。

还固定了箭头

window.onload = function() { // add window.onload here and set it euqal to a function
	  var timeOut;
    var slideIndex = 0;
    var slides;
	  showSlides(slideIndex);
		// Next/previous controls
		window.plusSlides =function (n) {
		   showSlides(n);
		}
		// Thumbnail image controls
		window.currentSlide = function(n) {
       slideIndex = (n -1)
	     showSlides();
		}
    
    function bind(){
     clearInterval(timeOut)
     timeOut =setInterval(showSlides, 5000);
    }

		function showSlides(n) {
		  var i;
		  slides = document.getElementsByClassName("mySlides");
		  var dots = document.getElementsByClassName("dot");
		  
		  for (i = 0; i < slides.length; i++) {
			 slides[i].style.display = "none";  
		  }
      if (n)
		    slideIndex += n;
       else slideIndex++;
		  if (slideIndex >= slides.length || slideIndex < 1) 
			{
			  slideIndex = 1
			}  
		  
		  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";
	    bind();
		}
    

		
		
	}
* {box-sizing:border-box}

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

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

/* Next & previous buttons */
.prev, .next {
  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;
  user-select: none;
}

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

/* 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);
}
*/
/* Caption text */
.text {
  color: #f2f2f2;
  
  font-size: 28px;
  padding: 8px 12px;
  position: absolute;
  bottom: 8px;
  width: 100%;
  text-align: center;
}

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

/* The dots/bullets/indicators */
.dot {
  cursor: pointer;
  height: 15px;
  width: 15px;
  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}
}
	<div class="slideshow-container">

		  <!-- Full-width images with number and caption text -->
		  <div class="mySlides fade">
			<img src="https://cdn.discordapp.com/attachments/502541850985496596/536997100823773184/image0.jpg" style="width:970px; height: 500px;">
			<div class="text"><span style = "background-color: black" >one on one weekly siminars</span></div>
		  </div>

		  <div class="mySlides fade">
			<img src="https://cdn.discordapp.com/attachments/502541850985496596/536997228791988244/image0.jpg" style="width:970px; height: 500px;">
			<div class="text"><span style = "background-color: black" >monthly seminars/workshops</span></div>
		  </div>

		  <div class="mySlides fade">
			<img src="https://cdn.discordapp.com/attachments/502541850985496596/536997338716176395/image0.jpg" style="width:970px; height: 500px;">
			<div class="text"><span style = "background-color: black" >nursing home visits</span></div>
		  </div>
		  
		  <div class="mySlides fade">
			<img src="pictures%20updated/42.jpg" style="width:970px; height: 500px;">
			<div class="text"><span style = "background-color: black" >community service</span></div>
		  </div>
		  
		  <div class="mySlides fade">
			<img src="pictures%20updated/21.jpg" style="width:970px; height: 500px;">
			<div class="text"><span style = "background-color: black" >college visits</span></div>
		  </div>

		  <!-- Next and previous buttons -->
		  <a class="prev" onclick="plusSlides(-1)">&#10094;</a>
		  <a class="next" onclick="plusSlides(1)">&#10095;</a>
		</div>
		<br>
		
		

		<!-- The dots/circles -->
		<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>