gpt4 book ai didi

具有可变超时的 Javascript 幻灯片

转载 作者:行者123 更新时间:2023-11-30 11:24:41 30 4
gpt4 key购买 nike

我有一个自动幻灯片放映效果很好。但是,我希望能够改变某些幻灯片的速度这是代码:

var slideIndex = 0;
showSlides();

function showSlides() {
var i;
var slides = document.getElementsByClassName("mySlides");
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
slideIndex++;
if (slideIndex > slides.length) {slideIndex = 1}
slides[slideIndex-1].style.display = "block";
setTimeout(showSlides, 2000); // Change image every 2 seconds
}

谢谢

最佳答案

我把你的代码放到了一个 almost-class 中。

fiddle :
https://jsfiddle.net/4bjcdftq/

片段:

var iSlider = {
slideIndex: null,
prevSlideIndex: null,
slides: null,
defaultDuration: 2000,

initSlides: function(){
this.slides = document.getElementsByClassName("iSlide");
for( var i = 0; i < this.slides.length; i++ ){
this.slides[i].style.display = "none";
}
this.prevSlideIndex = null;
this.slideIndex = -1;
},

showSlides: function(){
if( this.prevSlideIndex != null ) this.slides[ this.prevSlideIndex ].style.display = "none";
this.slideIndex = ( this.slideIndex + 1 + this.slides.length ) % this.slides.length;
this.slides[ this.slideIndex ].style.display = "block";
this.prevSlideIndex = this.slideIndex;
var duration = +( this.slides[ this.slideIndex ].getAttribute('data-duration') || this.defaultDuration );
setTimeout( this.showSlides.bind( this ), duration ); // Change image every 2 seconds
}
};

iSlider.initSlides();
iSlider.showSlides();
    <div class="iSlide" data-duration="500">111</div>
<div class="iSlide" data-duration="">222</div>
<div class="iSlide" data-duration="4000">333</div>
<div class="iSlide" data-duration="">444</div>

关于具有可变超时的 Javascript 幻灯片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48459700/

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