Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

199
Visualizações
Animation and transition property with setTimeout() function

I'm fairly new to JS, and I'm trying to make a simple text slide animation. However, when the animation event is activated the first time, the transition property doesn't apply. Every time after that it works fine.

So I'm struggling with the first execution of the slide animation.

Here is the code:

function slide_animation(direction){
    // Init
    let text = document.querySelector(".story .box .right-part .text");
    let text_html = text.innerHTML;
    let text_slide = [
        "Text n°1",
        'Text n°2',
        'Text n°3'
    ];
    let current_slide = 0;

    // Looking for the current_slide
    for(let i=0;i<text_slide.length;i++){
        if (text_slide[i]==text_html){current_slide = i};
    }

    // Calculating the next slide position
    if (direction=='right'){
        if (current_slide >= text_slide.length-1){
            current_slide = 0;
        }
        else {current_slide+=1;}
    }
    else {
        if (current_slide <= 0){
            current_slide = text_slide.length-1;
        }
        else {
            current_slide = current_slide-=1;
        }
    }

    // Animation
    setTimeout(()=>{
        text.style.transition = '0.5s';
        text.style.opacity = 0;
        text.style.left = '100px';  
    },250);


    setTimeout(()=>{
        text.innerHTML = text_slide[current_slide];
        text.style.opacity = 1;
        text.style.left = '0px';
    },750)
}
.story .box .right-part .text-container .text {
    line-height: 1.25;
    position: relative;
}
<div class="box">  
    <div class="left-part">
        <div class="text-container">                    
            <p class="text">
                <span class="text-padding fontsize-md">----</span>
            </p>
        </div>
        
        <div class="buttons-container">            
            <button class="backward" onclick="slide_animation('left')"></button>
            <button class="forward" onclick="slide_animation('right')"></button>
        </div>
    </div>
    
    <div class="right-part">
        <div class="text-container">
            <p class="text fontsize-sm">Text n°1</p>
        </div>
    </div>
</div>

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Anyway , it is fixed. I added another setTimeOut that did nothing really useful on the text , i placed it just before the others setTimeOut and it seems to work out properly.

Here is what is inserted :

setTimeout(()=>{
    text.style.left=0;
},0)
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda