我从w3schools的幻灯片制作“ HOW TO”指南开始,尽管它提供了自动幻灯片显示或带有控制箭头和幻灯片指示器的选项,但并没有告诉您如何同时进行。
在这里进行了一些搜索之后,我发现了这个(这篇文章底部的代码)-Automatic slideshow with button-但是解决方案有两个问题:
我尝试了许多解决方案,例如创建用于重置间隔计时器的函数,但是所有这些最终都破坏了代码,并使幻灯片显示变得混乱。我会在Codepen上发布一个链接,以给出一个示例,但是自从我今天早些时候写完笔以来,一旦我进入个人资料的“笔”页面,该站点就会停止工作。 (如果您真的有兴趣,请转到Codepen并查找我-用户名是lgcorpora-并尝试单击名为“带有按钮的自动幻灯片”的笔-应该在顶部附近。)
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>
答案 0 :(得分:0)
您必须在slideIndex
函数中设置currentSlide
。另外,将超时值放入变量(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)">❮</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>