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

348
Views
My JS animation resets its marginTop, but not its marginLeft

Im supernew to javascript animations. I just found out how to add the 2nd movement to my animation. When running funk() function, the ball goes down to the right. When it triggers funktre() function, the ball goes to its starting marginTop position but keeps its marginLeft position. Why does it return to the top? I want the 2nd animation, funktre(), to move the ball from the funk() ending position.

const ball = document.querySelector("#en");
ball.addEventListener("click", funk);

function funk() {
    let id = null;
    let position = 0;
    clearInterval(id);
    id = setInterval(funkto, 5);
        function funkto() {
            if (position == 450) {
                clearInterval(id);
                funktre();
                // sett en ny funksjon her
            }
            else {
                stop();
                position++;
                ball.style.marginLeft = position + "px";
                ball.style.marginTop = position + "px";
            }
        }
};

function stop() {
    ball.removeEventListener("click", funk);
};


function funktre() {
    let idd = null;
    let pos = 0;
    clearInterval(idd);
    idd = setInterval(funkfire, 5);
        function funkfire() {
            if (pos == 200) {
                clearInterval(idd);
                // ny funksjon her
            }
            else {
                pos++;
                ball.style.marginTop = pos + "px";
            }
        }
}

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

"I want the 2nd animation, funktre, to move the ball from the funk() ending position."

Then you should not initialize its starting position at zero, but at its current position.

function funktre() {
    let idd = null;
    let pos = parseInt(ball.style.marginTop); // set the initial pos to current pos
    clearInterval(idd);
    idd = setInterval(funkfire, 5);
        function funkfire() {
            if (pos == 200) {
                clearInterval(idd);
                // ny funksjon her
            }
            else {
                pos++;
                ball.style.marginTop = pos + "px";
            }
        }
}

If you intend to program more animations, or even a video game, I would suggest storing the ball top/left position in a global variable (which you could access from any scope).

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!