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

269
Visualizações
Nullify transforms (primarily rotation) in getBoundingClientRect?

I'm currently using this as a workout (just flat out removing the transform, getting the rect, and putting it back).

  useEffect(() => {
    if (typeof document !== undefined) {
      const ctn = document.getElementById(id);
      const parent = ctn.parentElement;
      const parentStyles = getComputedStyle(parent);
      const transform = parentStyles.transform;
      parent.style.transform = "rotate(0deg)";
      const box = parent.getBoundingClientRect(); 
      if (box.width > box.height * 0.5008) { // These numbers are specific to the aspect ratio of the phone frame, unrelated to question
        setHeight("100%");
        setWidth(box.height * 0.5008);
      } else {
        setHeight(box.width * 1.9888);
        setWidth("100%");
      }
      setBorderRadius(box.width * 0.125);
      parent.style.transform = transform;
    }
  }, [screen]);

The reason why I'm asking is because this is for a 'mock-up' component, i.e. you give it an image and it returns a nice mock-up. The general idea is that you just wrap it in whatever sized container you want, and it will auto-size everything with JS so the mock-up looks perfect. It's essentially 2 layers, the phone-frame img up top and the screen img below. I grab the width/height of the parent container and do a calculation to make the "screen" image line up perfectly with the phone.

No rotate, working good

If I don't completely remove the rotation transform before using getBoundingClientRect(), it calculates the width with the transform included, which screws up the element sizing. My current solution just feels sort of janky, and it makes you unable to change transforms on the parent container's CSS style sheet without a JS solution (because the transform just gets readded to inline-styles).

about 4 years ago · Juan Pablo Isaza
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