简体   繁体   English

单击幻灯片上的下一个或上一个按钮时创建新间隔

[英]make new interval when clicking on next or previous button on a slide

I have made an auto slide with interval of 8s, then i've added a previous and a next button that work but my problem now is that the interval is of course still running.我制作了一个间隔为 8 秒的自动幻灯片,然后我添加了一个可以工作的上一个和一个下一个按钮,但我现在的问题是间隔当然仍在运行。 Instead of that, I would like to make new interval each time I click on a button.取而代之的是,我想在每次单击按钮时设置新的间隔。 Could someone help me please?有人可以帮我吗? Thank you in advance:) have a good day提前谢谢你:) 祝你有美好的一天

javascript javascript

const prev = document.querySelector('.slider .fa-chevron-circle-left');
const next = document.querySelector('.slider .fa-chevron-circle-right');

setInterval(function() {
    var active = document.querySelector('.show');
    active.classList.remove('show');
    if (active.nextElementSibling && active.nextElementSibling !== next && active.nextElementSibling !== prev) {
        active.nextElementSibling.classList.add('show');
    } else {
        active.parentElement.firstElementChild.classList.add('show');
    }    
}, 8000);
    
next.addEventListener('click', function() {
    var nextSlide;
    var active = document.querySelector('.show');
    active.classList.remove('show');
    nextSlide = active.nextElementSibling && active.nextElementSibling !== next && active.nextElementSibling !== prev ? active.nextElementSibling.classList.add('show') : active.parentElement.firstElementChild.classList.add('show');
})

prev.addEventListener('click', function() {
    var prevSlide;
    var active = document.querySelector('.show');
    active.classList.remove('show');
    prevSlide = active.previousElementSibling && active.previousElementSibling !== next && active.previousElementSibling !== prev ? active.previousElementSibling.classList.add('show') : active.parentElement.children[2].classList.add('show');
})

html html

 <div class="slider">
    <div class="slide show"></div>
    <div class="slide"></div>
    <div class="slide"></div>
    <i class="fas fa-chevron-circle-left"></i>
    <i class="fas fa-chevron-circle-right"></i>
</div>

You should store the interval ID returned by setInterval function, then clear it in event handlers:您应该存储setInterval function 返回的间隔 ID,然后在事件处理程序中将其清除:

const intervalFunction = () => {
    var active = document.querySelector('.show');
    active.classList.remove('show');
    if (active.nextElementSibling && active.nextElementSibling !== next && active.nextElementSibling !== prev) {
      active.nextElementSibling.classList.add('show');
    } else {
      active.parentElement.firstElementChild.classList.add('show');
    }    
}

let intervalId = setInterval(intervalFunction, 8000);

next.addEventListener('click', function() {
    clearInterval(intervalId)
    intervalId = setInterval(intervalFunction, 8000);
    // ...
})

You can try this, I just clear the interval and restart the interval after the next/prev button clicked.你可以试试这个,我只是清除间隔并在单击下一个/上一个按钮后重新启动间隔。

function startSlider() {
    return setInterval(function() {
        var active = document.querySelector('.show');
        active.classList.remove('show');
        if (active.nextElementSibling && active.nextElementSibling !== next && active.nextElementSibling !== prev) {
            active.nextElementSibling.classList.add('show');
        } else {
            active.parentElement.firstElementChild.classList.add('show');
        }    
    }, 8000)
}

var slideInterval = startSlider();
    
next.addEventListener('click', function() {
    var nextSlide;
    var active = document.querySelector('.show');
    active.classList.remove('show');
    nextSlide = active.nextElementSibling && active.nextElementSibling !== next && active.nextElementSibling !== prev ? active.nextElementSibling.classList.add('show') : active.parentElement.firstElementChild.classList.add('show');
    clearInterval(slideInterval);
    slideInterval = startSlider();
})

prev.addEventListener('click', function() {
    var prevSlide;
    var active = document.querySelector('.show');
    active.classList.remove('show');
    prevSlide = active.previousElementSibling && active.previousElementSibling !== next && active.previousElementSibling !== prev ? active.previousElementSibling.classList.add('show') : active.parentElement.children[2].classList.add('show');
    clearInterval(slideInterval);
    slideInterval = startSlider();
})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Bootstrap 5仅在单击下一个和上一个按钮时使两个轮播同时滑动 - Bootstrap 5 make two carousels slide at the same time when clicking the next and previous buttons only 从反应应用程序的模态内部单击下一个和上一个按钮时,无法使动画(淡入淡出)在模态上工作 - unable to make the animation(fade) work on a modal when clicking the next and previous button from inside the modal in a react app 单击下一步按钮时删除以前的 JS function - Remove previous JS function when clicking next button 如何将下一张和上一张幻灯片的名称悬停在下一个和上一个按钮上? - How to hover the name of the next and previous slide on the next and previous button? 分别到达最后一张和第一张幻灯片后,如何使“下一个”按钮和“上一个”按钮不起作用? - How to make Next button and Previous button non-working after I reach last and first slide respectively? 单击下一个上一个的猫头鹰滑块会触发所有其他滑块滑动 - Owl Slider clicking on next previous triggers all other slider to slide 如何获取上一张和第一张幻灯片以激活下一个和上一个按钮或相应地禁用 - how to get last and first slide to make next and previous button active or disable accordingly 当Flickity到达下一张或上一张幻灯片时的事件 - Event for when Flickity reaches the next or previous slide 单击链接时使内容滑入和滑出? - make content slide in and out when clicking link? 单击按钮时,转到下一页并刷新上一个Jquery Mobile - When clicking a button go to next page and refresh the previous one Jquery Mobile
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM