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

396
Visualizações
transform: Scale , automatically reduces the size when using with javascript

eduReveal = 400;

var eduAni = false;

var education = document.getElementById('education');
var track = document.getElementById('track');
var plane = document.getElementById('plane');

window.addEventListener('scroll', function() {

  var topMost = window.innerHeight;
  var trackPos = education.getBoundingClientRect().top;

  if (eduAni == false && (topMost - eduReveal) > trackPos) {

    track.style.width = '100%';
    plane.style.transform = 'translate(800px)';

    plane.style.transitionDuration = '5s';
    track.style.transitionDuration = '5s';
    eduAni = true;
    console.log("Track");

  }

});
#trackBox {
  width: 800px;
  height: 2px;
  transform: rotate(-20deg);
}

#track {
  width: 0%;
  height: 100%;
  background-color: white;
}

#plane {
  color: white;
  position: relative;
  transform: scale(2);
}
<div id="trackBox">
  <div id="track"><i class="fa-solid fa-plane" id="plane"></i></div>
</div>

All the parts of the puzzle are given above. When I change the size of an element using transform: scale(), and then using javascript translate that element, its size turns down to normal again.

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

0

When you set the CSS transform property, you reset any transformations that were previously on it. So when you were setting your translate in JS, you were overwriting the scale that was set in your CSS. An easy solution is to store your transformations individually as custom properties like so:

eduReveal = 400;

var eduAni = false;

var education = document.getElementById('education');
var track = document.getElementById('track');
var plane = document.getElementById('plane');

window.addEventListener('scroll', function() {

  var topMost = window.innerHeight;
  var trackPos = education.getBoundingClientRect().top;

  if (eduAni == false && (topMost - eduReveal) > trackPos) {

    track.style.width = '100%';
    plane.style.transform = 'scale(var(--scale, 2)) translate(800px)';

    plane.style.transitionDuration = '5s';
    track.style.transitionDuration = '5s';
    eduAni = true;
    console.log("Track");

  }

});
body {
  min-height: 101vh;
  background-color: skyblue;
}

#trackBox {
  width: 800px;
  height: 2px;
  transform: rotate(-20deg);
}

#track {
  width: 0%;
  height: 100%;
  background-color: white;
}

#plane {
 --scale: 2;
  color: white;
  position: relative;
}
<div id="education">
  <div id="trackBox">
    <div id="track"><i class="fa-solid fa-plane" id="plane">Plane Icon</i></div>
  </div>
</div>

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