【问题标题】:Add autoplay to a css/html slideshow, is it possible?将自动播放添加到 css/html 幻灯片中,可以吗?
【发布时间】:2017-04-25 00:34:03
【问题描述】:

您好,我有以下带有控件的 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}
}

【问题讨论】:

    标签: css button slideshow autoplay


    【解决方案1】:

    假设您的幻灯片现在可以工作(您没有发布 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>

    【讨论】:

    • 亲爱的迈克尔,非常感谢您的完整回答,它现在完美运行。最好的问候!
    • 亲爱的@VictoriaG.,嗨!你很受欢迎。乐意效劳。和你聊天很棒。很快再和你说话!此致,迈克尔 :)
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2013-07-08
      • 2012-04-07
      • 2018-02-20
      • 1970-01-01
      • 2015-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多