Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

198
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!