在以下代码中,当我单击导航或指示器时,幻灯片显示加速。如何使控件正常工作但保持相同的时间?
单击左箭头时,幻灯片应该返回一张幻灯片。单击右箭头时,幻灯片应在幻灯片上前进。单击指示符(1,2,3,4)时应选择幻灯片。然后恢复当前时间。
{{ csrf_token }}

var slideIndex = 0;
showSlides(slideIndex);
function plusSlides(n) {
showSlides(slideIndex += n);
}
function currentSlide(n) {
showSlides(slideIndex = n);
}
function showSlides() {
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;}
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, 10000); // 1000 = 1 sec
}
&#13;
/* Slideshow container */
.slideshow-container {
width: 600px;
position: relative;
margin: auto;
/*top: 50px;*/
height: 600px;
}
.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;
}
&#13;
答案 0 :(得分:1)
首先,您不需要输入参数showSlides
。
您遇到的问题是当用户单击导航器或箭头时,您的函数会递归触发setTimeout(showSlides, 10000);
。
以下代码通过清除当前setTimeout
任务重置计时器。
var slideIndex = 0;
var currentTask = null;
showSlides();
function plusSlides(n) {
slideIndex += n;
showSlides();
}
function currentSlide(n) {
slideIndex = n;
showSlides();
}
function showSlides() {
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;}
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";
if (currentTask) {
clearTimeout(currentTask);
currentTask = null;
}
currentTask = setTimeout(showSlides, 10000); // 1000 = 1 sec
}
/* Slideshow container */
.slideshow-container {
width: 600px;
position: relative;
margin: auto;
/*top: 50px;*/
height: 600px;
}
.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)">❮</a>
<div style="text-align:center; padding-top: 30px;">
<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)">❯</a>
<!--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>
答案 1 :(得分:1)
您的方法中有几处错误。以下是基于 setInterval 函数的解决方案:
var slideIndex = 1;
var millis = 2000;
nextSlide();
var interval = setInterval(nextSlide, millis);
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;
/*top: 50px;*/
height: 600px;
}
.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)">❮</a>
<div style="text-align:center; padding-top: 30px;">
<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)">❯</a>
<!--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>
答案 2 :(得分:0)