getting the X and Y coordinates for a div element

前端 未结 5 1811
故里飘歌
故里飘歌 2020-12-17 14:55

I\'ve been trying to make a javascript to get a X and Y coordinates of a div element. After some trying around I have come up with some numbers but I\'m not sur

5条回答
  •  执笔经年
    2020-12-17 15:35

    Given the element...

    Some text

    If the element is in the main document you can get the DIV's coordinates with...

     var X=window.getComputedStyle(abc,null).getPropertyValue('left');
    
     var Y=window.getComputedStyle(abc,null).getPropertyValue('top');
    

    If the element is in an iframe you can get the DIV's coordinates with...

     var X=FrameID.contentWindow.getComputedStyle(abc,null).getPropertyValue('left');
    
     var Y=FrameID.contentWindow.getComputedStyle(abc,null).getPropertyValue('top');
    

    NB: The returned values should be in the format "190px" and "350px".

提交回复
热议问题