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

129
Visualizações
How to calculate window width based on getBoundingClientRectAsync values of another element?

Is it possible to calculate viewport or window width based on getBoundingClientRectAsync values of element which is in virtual DOM?

Running JS in web worker (amp-script):

let button = document.querySelector('.tooltip-trigger');

async function addTooltip(event) {

    tooltipElem = document.createElement('span'); 
    document.body.appendChild(tooltipElem);

    let coordsTrigger = await target.getBoundingClientRectAsync();

    // How to get right offset?
    let windowWidth = coordsTrigger.left + coordsTrigger.width;
    console.log(windowWidth);
    
}

Target Element (wrapped in amp-script):

<amp-script layout="fill" script="tooltip-async"><span class="tooltip-trigger"></span></amp-script>

enter image description here

The half working solution is to use window.innerWidth. Half working, because window.innerWidth value changes only after page reload, and is not changing on resize or any other event for some reason. Probably because it is running in amp-script web-worker DOM and not in main;

coordsTrigger out:

bottom: 383.65625
height: 24
left: 122.59375
right: 201.640625
top: 359.65625
width: 79.046875
x: 359.65625
y: 122.59375
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