gpt4 book ai didi

javascript - 单击导航或指示器时,幻灯片放映速度会加快

转载 作者:太空宇宙 更新时间:2023-11-04 01:56:24 25 4
gpt4 key购买 nike

在以下代码中,当我单击导航或指示器时,幻灯片放映速度会加快。如何让控件正常工作但保持相同的时间?

当点击向左箭头时,幻灯片应该返回一张幻灯片。单击向右箭头时,幻灯片应在幻灯片上前进。单击指标 (1、2、3、4) 时,应选择该幻灯片。然后恢复当前计时。

 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
}
/* 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)">&#10094;</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)">&#10095;</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>

最佳答案

您的方法有几个错误。以下是基于 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)">&#10094;</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)">&#10095;</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>

关于javascript - 单击导航或指示器时,幻灯片放映速度会加快,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42543576/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com