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

263
Visualizações
If an elements style.left reaches something, change style.left to something else

I'm currently working on creating a simple interactive CV, with mainly javascript. The idea is that the user steers a character to the right and then a few "about me"-divs will show up. These "about-me"-divs are supposed to move when the character moves, so it looks like they're getting closer to/further away from them. If unclear, check the demo.

I've fixed this by setting style.left to 100, and then subtracting from it each time the character moves to the right. In the example I've used minions, instead of "about-me"-divs, just to get the function to work.

let moveMinion = 100; // This is the same as their 'left' in CSS.
let moveMinionTwo = 150;

if (keyPresses.ArrowRight) {
        moveMinion -= 0.3;
        moveMinionTwo -= 0.3;
        
        minionOne.style.left = moveMinion + '%'; // I Used % because I want it to work on different screen widths.
        minioneTwo.style.left = moveMinionTwo + '%'; }

However I want the "about me"-divs to "repeat" and come back again, after the character have walked for a bit. Like this example.

I tried using this code underneath, in the same function ofc, but nothing happens, except it seems like the minion blinks quickly when style.left is -10%.

if (minionOne.style.left <= -10 + '%') minionOne.style.left = moveMinion + '%';

TLDR: I want style.left to change to something else when style.left hits a specific number.

Bear in mind, this is not finished yet, so you don't have to judge the code and the project outside of the one I need help with, unless you want to give any tips.

Repo on github

My demo

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can't <= strings. You gotta convert one to a number and compare to a number.

const threshold = -10;
const asNum = Number(minionOne.style.left.split('%')[0]);
if (asNum <= threshold ) {
  minionOne.style.left = `${100 + asNum - threshold}%`;
}
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