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

351
Visualizações
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 Respostas
Responde à pergunta

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 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