【发布时间】:2018-11-07 03:47:38
【问题描述】:
我从 w3schools 关于制作幻灯片的“如何”指南开始,虽然它提供了自动幻灯片或带有控制箭头和幻灯片指示器的选项,但它并没有告诉您如何同时进行。
经过一番搜索,我找到了这个(本文底部的代码)——Automatic slideshow with button——但解决方案有两个问题:
- 如果我点击底部不是自动显示的下一个指示按钮,幻灯片将成功跳转到该项目,但不会继续到我下一个的幻灯片'刚刚点击;相反,如果我根本没有点击过按钮,它将转到下一个要显示的幻灯片。如果我使用箭头,也会存在同样的问题;底部按钮只是更容易用作示例。
前任。我有幻灯片,标记为 A、B、C 和 D。页面加载并从幻灯片 A 开始。指定的时间间隔过去了,幻灯片自动移动到 B。我单击幻灯片指示器按钮将自己移动到幻灯片 A ,它会将我移动到 A。然后页面将我带到 C,就好像我从未点击过 A。我希望幻灯片从我单击它的位置 (A) 开始,而不是从在我点击它之前它“打算”去的下一个位置 (C)。 - 如果我在计数器中的某些时间过去后单击任何按钮更改幻灯片,则计数器不会重置;它会继续,就好像我没有更改幻灯片一样,然后当它再次自动移动时重新开始。
前任。自动换片前的时间为 5 秒。我在查看幻灯片 A 仅 3 秒后单击向右箭头。然后幻灯片显示为幻灯片 B 但仅在继续播放幻灯片 C 之前的剩余 2 秒内显示。
我尝试了许多解决方案,例如创建函数来重置间隔计时器,但所有这些最终都破坏了代码并使我的幻灯片放映变得很糟糕。我会发布一个指向我的 Codepen 的链接来举例说明,但是自从我今天早些时候制作了这支笔后,一旦我进入我个人资料的“笔”页面,该网站就会停止工作。 (如果你真的感兴趣,去 Codepen 找我——用户名是 lgcorpora——然后尝试点击名为“带按钮的自动幻灯片”的 Pen——应该就在顶部附近。)
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)">❮</a>
<a class="next" onclick="plusSlides(1)">❯</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 html css