Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

121
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!