Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why my Javascript animation doesn't work?

I want to make a small animation to make a div(id="dice") grow in width, but I don’t know what’s going wrong. The position is already absolute. I have tried to create a button to test it (id="Pr_btn").

const pr_btn=document.getElementById("Pr_btn");
pr_btn.addEventListener("click", VtnDice);

function VtnDice(){
    let interv = null;
    const dice=document.getElementById("dice");
    let diceWidth=0;
    clearInterval(interv);
    interv=setInterval(frame, 5);

    function frame(){
        if(diceWidth<300){
            clearInterval(interv);
        }else{
            diceWidth++;
            dice.style.width= diceWidth + "px";
        }
    }
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The solution is very simple ! Your condition to stop the growth is the wrong way around 🙂
Replace diceWidth<300 with diceWidth>=300 and it should work like a charm !

I did a fiddle there : https://jsfiddle.net/4ubvm1a7/1/

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading