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>
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