将自动播放添加到 css/html 幻灯片,这可能吗?

Add autoplay to a css/html slideshow, is it possible?

您好,我有以下 css- html 带控件的幻灯片,但我也想添加自动播放功能。可能吗?我试过 javascript 但我没有运气。我想留下箭头,把下面的子弹去掉。

<div class="slideshow-container">
<div class="mySlides fade">
<img src="lara_zizektours.jpg" name="slide"  alt="" style="width:100%">
</div>
<div class="mySlides fade">
<img src="tremor_zizektours.jpg"  name="slide" alt="" style="width:100%">
</div>
<div class="mySlides fade">
<img src="femina_zizektours.jpg" name="slide" alt="" style="width:100%">
</div>
<div class="mySlides fade">
<img src="kingcoya_zizektours.jpg"  name="slide" alt="" style="width:100%">
</div>
<div class="mySlides fade">
<img src="sofia_zizektours.jpg"  name="slide" alt="" style="width:100%">
</div>
<div class="mySlides fade">
<img src="1_zizektours.jpg" alt=""  name="slide" style="width:100%">
</div>


<a class="prev" onclick="plusSlides(-1)">&#10094;</a>
 <a class="next" onclick="plusSlides(1)">&#10095;</a>
</div>
<br>
<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> 
<span class="dot" onclick="currentSlide(5)"></span> 
<span class="dot" onclick="currentSlide(6)"></span> 
</div>





   /* SLIDESHOW CONTAINER */
.slideshow-container {
max-width: 2000px;
position: relative;
margin: 0px;
}

.mySlides {
display: none;
}

/* Next & previous buttons */
.prev, .next {
cursor: pointer;
position: absolute;
top: 50%;
width: auto;
margin-top: -22px;
padding: 16px;
color: yellow;
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: yellow;
}

/* Fading animation */
.fade {
-webkit-animation-name: fade;
-webkit-animation-duration: 1.5s;
animation-name: fade;
animation-duration: 1.5s;
}

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

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

假设您的幻灯片现在可以播放(您没有 post javascript),您可以设置一个简单的 setInterval 并在您的其中一个箭头键上触发 click() .

我为每个箭头添加了一个 ID,以便在 javascript 中更容易定位。我在这里添加了一个 plusSlides() 函数只是为了 console.log 事件触发 - 你不需要它,因为你应该已经在你的代码中有它了。

var next = document.getElementById('next'),
    seconds = 3;

var interval = setInterval(function() {
  next.click();
}, (seconds * 1000))

/* you don't need this function - just for the demo */
function plusSlides(val) {
    console.log(val);
}
<a class="prev" id="prev" onclick="plusSlides(-1)">&#10094;</a>
<a class="next" id="next" onclick="plusSlides(1)">&#10095;</a>

https://www.w3schools.com/w3css/w3css_slideshow.asp 在自动幻灯片放映标题下提供了答案。您可以比较并重用他们的代码和脚本。