gpt4 book ai didi

javascript - 带箭头和按钮的自动幻灯片

转载 作者:行者123 更新时间:2023-11-28 02:27:21 24 4
gpt4 key购买 nike

我从 w3schools 的制作幻灯片的“HOW TO”指南开始,虽然它提供了自动幻灯片或带有控制箭头和幻灯片指示器的选项,但它没有告诉您如何同时进行。

在这里搜索之后,我找到了这个(代码在这篇文章的底部)-- Automatic slideshow with button -- 但解决方案存在两个问题:

  1. 如果我点击底部不是下一个自动显示的指示器按钮,幻灯片将成功跳转到该元素,但不会继续到我下一张幻灯片之后的幻灯片刚刚点击;相反,如果我根本没有点击过按钮,它将转到下一张要显示的幻灯片。如果我使用箭头,也存在同样的问题;作为示例,底部按钮更易于使用。
    前任。我有幻灯片,分别标记为 A、B、C 和 D。页面加载并从幻灯片 A 开始。指定的时间间隔过去了,幻灯片自行移动到 B。我单击幻灯片指示器按钮将自己移动到幻灯片 A ,它会将我移动到 A。然后页面将我带到 C,就好像我从未单击过 A。我希望幻灯片从我单击它 (A) 开始的位置继续,而不是从在我点击它之前它“打算”去的地方 (C)。
  2. 如果我在计数器中的一些时间过去后单击任何按钮来更改幻灯片,计数器不会重置;它会继续,就好像我没有更改幻灯片一样,然后当它再次自动移动时它会重新开始。
    前任。自动换玻片前的时间为 5 秒。我在观看幻灯片 A 仅 3 秒后单击向右箭头。然后幻灯片显示为幻灯片 B 但仅在剩余的 2 秒内继续播放幻灯片 C。

我尝试了多种解决方案,例如创建函数来重置间隔计时器,但所有这些解决方案最终都破坏了代码并使我的幻灯片放映变得糟糕。我会发布一个指向我的 Codepen 的链接来举个例子,但是自从我今天早些时候制作了这支笔,一旦我进入我的个人资料的“笔”页面,该网站就停止工作。 (如果您真的感兴趣,请转到 Codepen 并查找我的资料——用户名是 lgcorpora——然后尝试单击名为“Automatic slideshow with buttons”的笔——应该就在顶部附近。)

var slideIndex = 0;
showSlides();
var slides,dots;

function showSlides() {
var i;
slides = document.getElementsByClassName("mySlides");
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, 4000); // Change image every 8 seconds
}

function plusSlides(position) {
slideIndex +=position;
if (slideIndex> slides.length) {slideIndex = 1}
else if(slideIndex<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";
}

function currentSlide(index) {
if (index> slides.length) {index = 1}
else if(index<1){index = 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[index-1].style.display = "block";
dots[index-1].className += " active";
}
#slide{
width:96%;
}

* {
box-sizing: border-box
}

.mySlides {
display: none
}

.slideshow-container {
width: auto;
position: relative;
margin: -10px;

}
/* Next & previous buttons */
.prev, .next {
cursor: pointer;
position: absolute;
top: 50%;
width: auto;
padding: 16px;
margin-top: -22px;
color: #494B55;
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(0,0,0,0.8);
}
/* Caption text */
.text {
color: #f2f2f2;
font-size: 15px;
padding: 8px 12px;
position: absolute;
bottom: 8px;
width: 100%;
text-align: center;
}

/* The dots/bullets/indicators */
.dot {
cursor: pointer;
height: 13px;
width: 13px;
margin: 0 2px;
background-color: #bbb;
border-radius: 50%;
display: inline-block;
transition: background-color 0.6s ease;
}
.active, .dot:hover {
background-color: #494B55;
}
/* Fading animation */
.fade {
-webkit-animation-name: fade;
-webkit-animation-duration: 2s;
animation-name: fade;
animation-duration: 2s;
}
@-webkit-keyframes fade {
from {opacity: .4}
to {opacity: 1}
}

@keyframes fade {
from {opacity: .4}
to {opacity: 1}
}

/* On smaller screens, decrease text size */
@media only screen and (max-width: 300px) {
.text {font-size: 11px}
}

.pics
{
border:3px solid #494B55;
width:200px;
}
<div class="ss-container">
<div class="mySlides fade">
<img src="https://www.w3schools.com/css/img_5terre.jpg" class="pics"/>
</div>
<div class="mySlides fade">
<img src="https://www.w3schools.com/css/img_forest.jpg" class="pics"/>
</div>
<div class="mySlides fade">
<img src="https://www.w3schools.com/css/img_lights.jpg" class="pics"/>
</div>
<div class="mySlides fade">
<img src="https://www.w3schools.com/css/img_mountains.jpg" class="pics"/>
</div>
<a class="prev" onclick="plusSlides(-1)">&#10094;</a>
<a class="next" onclick="plusSlides(1)">&#10095;</a>
</div>

<!-- Putting in the click dots -->
<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>
</div>

最佳答案

您必须在 currentSlide 函数中设置 slideIndex。此外,将超时设置到一个变量 (timer) 中,以便您可以在手动显示幻灯片时停止它。

var slideIndex = 0;
showSlides();
//add the global timer variable
var slides,dots,timer;

function showSlides() {
var i;
slides = document.getElementsByClassName("mySlides");
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";
//put the timeout in the timer variable
timer = setTimeout(showSlides, 4000); // Change image every 8 seconds
}

function plusSlides(position) {
//clear/stop the timer
clearTimeout(timer);
slideIndex +=position;
if (slideIndex> slides.length) {slideIndex = 1}
else if(slideIndex<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";
//create a new timer
timer = setTimeout(showSlides, 4000);
}

function currentSlide(index) {
//clear/stop the timer
clearTimeout(timer);
if (index> slides.length) {index = 1}
else if(index<1){index = slides.length}
//set the slideIndex with the index of the function
slideIndex = index;
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[index-1].style.display = "block";
dots[index-1].className += " active";
//create a new timer
timer = setTimeout(showSlides, 4000);
}
#slide{
width:96%;
}

* {
box-sizing: border-box
}

.mySlides {
display: none
}

.slideshow-container {
width: auto;
position: relative;
margin: -10px;

}
/* Next & previous buttons */
.prev, .next {
cursor: pointer;
position: absolute;
top: 50%;
width: auto;
padding: 16px;
margin-top: -22px;
color: #494B55;
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(0,0,0,0.8);
}
/* Caption text */
.text {
color: #f2f2f2;
font-size: 15px;
padding: 8px 12px;
position: absolute;
bottom: 8px;
width: 100%;
text-align: center;
}

/* The dots/bullets/indicators */
.dot {
cursor: pointer;
height: 13px;
width: 13px;
margin: 0 2px;
background-color: #bbb;
border-radius: 50%;
display: inline-block;
transition: background-color 0.6s ease;
}
.active, .dot:hover {
background-color: #494B55;
}
/* Fading animation */
.fade {
-webkit-animation-name: fade;
-webkit-animation-duration: 2s;
animation-name: fade;
animation-duration: 2s;
}
@-webkit-keyframes fade {
from {opacity: .4}
to {opacity: 1}
}

@keyframes fade {
from {opacity: .4}
to {opacity: 1}
}

/* On smaller screens, decrease text size */
@media only screen and (max-width: 300px) {
.text {font-size: 11px}
}

.pics
{
border:3px solid #494B55;
width:200px;
}
<div class="ss-container">
<div class="mySlides fade">
<img src="https://www.w3schools.com/css/img_5terre.jpg" class="pics"/>
</div>
<div class="mySlides fade">
<img src="https://www.w3schools.com/css/img_forest.jpg" class="pics"/>
</div>
<div class="mySlides fade">
<img src="https://www.w3schools.com/css/img_lights.jpg" class="pics"/>
</div>
<div class="mySlides fade">
<img src="https://www.w3schools.com/css/img_mountains.jpg" class="pics"/>
</div>
<a class="prev" onclick="plusSlides(-1)">&#10094;</a>
<a class="next" onclick="plusSlides(1)">&#10095;</a>
</div>

<!-- Putting in the click dots -->
<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>
</div>

关于javascript - 带箭头和按钮的自动幻灯片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53178768/

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