I am trying to match the position and size of two elements in two different iframes under the same parent page, but I am not sure how to do it.
With the following code, I manage to match the position and size of a div ("div_parent_page') in the parent page with the position of an element ('element_in_iframe-1") inside an iframe ('iframe-1') under the same parent page:
function myFunction() {
function getOffset( el ) {
var _x = 0;
var _y = 0;
while( el && !isNaN( el.offsetLeft ) && !isNaN( el.offsetTop ) ) {
_x += el.offsetLeft - el.scrollLeft;
_y += el.offsetTop - el.scrollTop;
el = el.offsetParent;
}
return { top: _y, left: _x };
}
var positionInfo = ( document.getElementById('iframe-
1').contentWindow.document.getElementById('element_in_iframe-1')
).getBoundingClientRect();
var yheight = positionInfo.height;
var xwidth = positionInfo.width;
var x = getOffset( document.getElementById('iframe-
1').contentWindow.document.getElementById('element_in_iframe-1') ).left;
var y = getOffset( document.getElementById('iframe-
1').contentWindow.document.getElementById('element_in_iframe-1') ).top;
var d = document.getElementById('div_parent_page');
d.style.position = "absolute";
d.style.left = x+'px';
d.style.top = y+'px';
d.style.width = xwidth;
d.style.height = yheight; `
}
var timer = setInterval(myFunction, 1000);
Now, I put another iframe ('iframe-2') inside the div mentioned above, and my goal is to match the position and size of an element ('elementXXX') inside iframe-2 inside the div with the position and size of element_in_iframe-1. I added the code below:
var v = document.getElementById('iframe-
2').contentWindow.document.getElementsByClassName('elementXXX');
v.style.position = "absolute";
v.style.left = x+'px';
v.style.top = y+'px';
v.style.width = xwidth;
v.style.height = yheight;
}
Apparently, it did not work, but I am not sure how this can be achieved. Hope you guys can enlighten me because I am really a Grandma in coding. Thanks~