繁体   English   中英

如何在Vanilla JavaScript中实现Promise以等待转换完成?

[英]How to implement promises in vanilla javascript to wait for transitions to complete?

我在遍历项目列表并通过香草javascript淡入和淡出时遇到困难。 假设我有一个字符串列表[“ cat”,“ ball”,“ bat”,“ mouse”]。

我想遍历这些项目并逐一显示它们。 例如,首先我需要显示“ cat”,然后继续显示“ bat”。 为此,我必须首先等待,直到“ cat”淡入,等待它淡出,然后显示“ bat”。 当前,我只使用一个for循环,该循环直接进入列表“ mouse”的末尾,而不等待其他元素完成淡入淡出。

为了解决这个问题,我知道我需要使用异步编程,回调,promise API等,但是我并没有真正使用它们,所以我不知道如何实现此解决方案。 非常感谢您对如何与“ setInterval()”一起使用异步编程的任何帮助或澄清。

这是我的代码片段:

var textarr = ["cat", "ball", "bat", "mouse"]
for(let i=0; i<textarr.length; i++){
  var text_item = document.getElementById('text_item');
  text_item.style.opacity = 0;
  text_item.innerHTML = textarr[i];
  // problem lies here; this is skipping to end of the list (mouse)
  fadeIn(text_item);
}

//function borrowed from stack overflow to fadeIn elements

function fadeIn(element) {
    var op = 0.1;  // initial opacity
    element.style.display = 'block';
    var timer = setInterval(function () {
        if (op >= 1){
            clearInterval(timer);
        }
        element.style.opacity = op;
        element.style.filter = 'alpha(opacity=' + op * 100 + ")";
        op += op * 0.1;
    }, 100);
}

利用@RwwL在注释中提到的transitionend事件:

 let display = document.getElementById('display'); const animals = ['dog', 'cat', 'mouse']; let i = 0; display.addEventListener('transitionend', function(event) { if (i < animals.length) { if (!display.classList.contains('fadeIn')) { display.innerHTML = animals[i++]; } display.classList.toggle('fadeIn'); } }, false); // Set timeout, otherwise the transition won't take effect (there are ways around this) setTimeout(() => { display.classList.toggle('fadeIn'); }, 1000); 
 #display { color: #FFFFFF; transition: color 1s ease-in; } #display.fadeIn { color: #000000; } 
 <div id="display">Animal Names Here</div> 

最好的方法是使用队列,其中计时器从列表中选择项目,然后执行操作。 在这种情况下,我使用CSS过渡对其进行了编码,以进行简单的淡入淡出。

 // Made it a function so you could use // it more than once on same page function changeText (arr, elem, startIndex) { // holds active slide let current = startIndex || 0 // Called when we what to show the current word const update = function () { // set the word on the page elem.innerHTML = arr[current] // Add the class to start transition elem.classList.add('fadeIn') // How long you want the word to stay on the screen // When timer executes it calls function to reverse // the annimation window.setTimeout(next, 3000) // 3 seconds } // Called to do the reverse animation and update the step const next = function () { // remove the class so goes back to zero elem.classList.remove('fadeIn') // update what slide we are on current++ // If we are over the limit, than restart if (current===arr.length) { current = 0 // back to first slide } // How long do you want for the fade out transition to // take to execute. window.setTimeout(update, 300); // 300 milliseconds } // initialize it so first word shows up update(); } var textarr = ["cat", "ball", "bat", "mouse"] changeText(textarr, document.querySelector('.text1')) changeText(textarr, document.querySelector('.text2'), 3) 
 .text { opacity: 0; transition: opacity .3s ease-in-out; } .fadeIn { opacity: 1; transition: opacity 2s ease-in-out; } 
 <h1>One</h1> <div class="text text1"></div> <h2>Another one</h2> <div class="text text2"></div> 

暂无
暂无

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

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