Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

122
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda