How to display sequential DIVs using fade and time control?

0

Good afternoon, I want to make a sequence of DIVs displayed one by one during a time interval, in the same location, including fade in and fade out. I ask: how to use JS to determine this interval and follow the execution?

Example:

Here the content will appear:

<div id="local"></div>

This is the content:

<div id="tela1" style="opacity:1; transition: 10s">Tela 1</div>
<div id="tela2" style="opacity:0">Tela 2</div>
<div id="tela3" style="opacity:0">Tela 3</div>

Desired operation:

  • Displays "screen1" in "location"
  • Wait 10 seconds
  • Fade out and fade in
  • Displays "screen2" in "location"
  • Wait 10 seconds
  • Fade out and fade in
  • Repeat the process until the contents of all DIVs are displayed
  • Thank you!

        
    asked by anonymous 30.01.2018 / 20:20

    1 answer

    1

    You can use a asynchronous function to achieve this effect. To do this, simply create a function with the keyword async on the front. Ex:

    (async () => {
      /* Code here */
    })();
    

    This will allow you to use the await operator. > to be able to pause the function for a while. Ex:

    await espera_x_segundos();
    

    Now you can create a espera_x_segundos function with a setTimeout . The await will wait for this time to end and then continue with the code.

    For animation you can use @keyframes of CSS. Ex:

    #local {
      animation-duration: 2s;
      animation-name: fadeout;
    }
    
    @keyframes fadein {
        from {opacity: 0}
        to {opacity: 1}
    }
    

    With this you can make an effect of fadein or fadeout easily.

    Here is a complete example:

    let telas = document.querySelectorAll(".tela");
    const local = document.querySelector("#local");
    
    (async () => {
      for (let tela of telas) {
        local.innerHTML = tela.outerHTML;
        local.classList.add("active")
        await wait(2000)
        local.classList.remove("active")
        await wait(2000)
      }
    })();
    
    function wait(ms) {
        return new Promise(resolve => setTimeout(resolve, ms));
    }
    #local {
      animation-duration: 2s;
      animation-name: fadeout;
    }
    
    #local.active {
      animation-duration: 2s;
      animation-name: fadein;
    }
    
    @keyframes fadein {
      from {opacity: 0}
      to {opacity: 1}
    }
    
    @keyframes fadeout {
      from {opacity: 1}
      to {opacity: 0}
    }
    <div id="tela1" class="tela">Tela 1</div>
    <div id="tela2" class="tela">Tela 2</div>
    <div id="tela3" class="tela">Tela 3</div>
    
    <hr />
    
    <div id="local">Olá Brazil</div>

    Alternative with jQuery

    let telas = document.querySelectorAll(".tela");
    
    (async () => {
      for (let tela of telas) {
        $("#local").empty().html( tela.outerHTML )
          .fadeIn(1000)    // Duração do efeito FadeIn
          .delay(3000)     // Delay antes da próxima execução
          .fadeOut(1000);  // Duração do efeito FadeOut
          
        await wait(5000);  // Soma dos valores acima
      }
    })();
    
    function wait(ms) {
        return new Promise(resolve => setTimeout(resolve, ms));
    }
    #local {
      display: none
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divid="tela1" class="tela">Tela 1</div>
    <div id="tela2" class="tela">Tela 2</div>
    <div id="tela3" class="tela">Tela 3</div>
    
    <hr />
    
    <div id="local"></div>
        
    30.01.2018 / 21:30