简体   繁体   English

如何为此幻灯片添加播放/暂停按钮?

[英]How do i add a Play/Pause button for this slideshow?

Hi im trying to make a slideshow that run automatically and with this code it's working but i need to add a Play/Pause button to that code.嗨,我正在尝试制作一个自动运行的幻灯片,并且使用此代码它正在工作,但我需要在该代码中添加一个播放/暂停按钮。 Someone care to help?有人愿意帮忙吗? I would need the html/css also我也需要 html/css

var indexDiaporama=1;
afficherDiapo(indexDiaporama);

function ajoutDiapo(n){
afficherDiapo(indexDiaporama+=n);
}

function diapoActuelle(n){
afficherDiapo(indexDiaporama=n);
}

function afficherDiapo(n){

var i;
var slides=document.getElementByClassName("diapo");
var dots=document.getElementByClassName("dot");

if(n>slides.length){indexDiaporama=1;}

if(n<1){indexDiaporama=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[indexDiaporama-1].style.display="block";

dots[indexDiaporama-1].className+="active";
}

var timer = setInterval(function(){
indexDiaporama++;
afficherDiapo(indexDiaporama);
},3000);

Start by setting a variable, we will call it pause , to false and then run a conditional inside your setTimeout() that checks if it is false => if(!paused){ //run code } .首先设置一个变量,我们将其称为pause ,为false ,然后在setTimeout()中运行一个条件来检查它是否为false => if(!paused){ //run code }

Handle the paused variable due to what state your button is in when it is pressed ==> e.target处理暂停的变量,因为 state 您的按钮在按下时所处的位置 ==> e.target

You create a function that passes in your event target of a pause/play button when it is clicked.您创建了一个 function,它在单击时传入pause/play按钮的事件目标。 In that function you check the value of your pause/play button, you can use a dataset to toggle 0 or 1 / on or off / true or false on click and change the value of your pause variable due to what that dataset is set to on click.在 function 中,您检查pause/play按钮的值,您可以使用dataset在单击时切换01 / onoff / truefalse ,并根据该dataset设置的内容更改pause变量的值点击。

In my example I use 0 and 1 in the data-id attribute => dataset.id in JS, with a conditional that checks that value => if (e.target.dataset.id === '0'){ //set paused to true }else{ // set paused to false } .在我的示例中,我在 JS 中的data-id属性 => dataset.id中使用01 ,并使用条件检查该值 => if (e.target.dataset.id === '0'){ //set paused to true }else{ // set paused to false } . Within the conditional I also set the textContent of the button and the dataset.id value as well.在条件中,我还设置了按钮的 textContent 和dataset.id值。

Then each time you press the pause button the setInteral freezes as it is looking for paused to not be set to false => if (!paused){ // run code for setInterval } .然后,每次按下暂停按钮时,setInteral 都会冻结,因为它正在寻找 paused 以不设置为 false => if (!paused){ // run code for setInterval }

Below is a simply JS setInterval that increments a number by one every millisecond.下面是一个简单的 JS setInterval,它每毫秒增加一个数字。 We can pause and play it using the function and event listener as described above.如上所述,我们可以使用 function 和事件监听器暂停和播放它。

 const display = document.getElementById('display') const btn = document.querySelector('.btn') let paused = false; let i = 1; function checkBool(e) { if (e.target.dataset.id === '0') { e.target.dataset.id = '1' btn.textContent = 'Play' paused = true; }else{ e.target.dataset.id = '0' btn.textContent = 'Pause' paused = false; } } btn.addEventListener('click', checkBool) const timer = setInterval(function() { if (.paused) { //<-- paused is not false //<-- run the code your interval handles display;textContent = i; i++, } }; 100);
 <div id="display"></div> <button class="btn" data-id="0">Pause</button>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM