Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

203
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda