Exclude html element position in iframe

I want to know the position of an element that is in an iframe. I am trying to use: -

//Function to find the position of element on page function getElementPosition(elem){ var posX = 0; var posY = 0; while(elem!= null){ posX += elem.offsetLeft; posY += elem.offsetTop; elem = elem.offsetParent; console.log("In function:" + elem.tagName + " " + posX + " " + posY); } return { x : posX, y : posY }; } 

To get a list of all iframes,

 var doc1 = $('#page1').get(0).contentDocument; // page1 is id of iframe element var list1 = doc1.getElementsByTagName('*'); console.log(list1.length); // Printing correctly var index = prompt("Enter element index"); var elem1 = list1[index]; var pos1 = getElementPosition(elem1); console.log("Positions:" + pos1.x + " " + pos1.y); 

But that does not work. Elem is not initially null, but elem.offsetParent is null in each case. Am I something wrong?

For some elements, it works fine. But for some elements, offsetParent is null, and therefore their offsetLeft is 0, and offetTop is 0.

Is there any other way to find out the position of each element.

Thanks in advance.

+6
source share
1 answer

You are already using jQuery. Why not let jQuery do the heavy lifting for you?

http://api.jquery.com/position/

http://api.jquery.com/offset/

0
source

Source: https://habr.com/ru/post/917419/


All Articles