Retrieve the position (X,Y) of an HTML element relative to the browser window

前端 未结 27 3792
闹比i
闹比i 2020-11-21 04:59

I want to know how to get the X and Y position of HTML elements such as img and div in JavaScript relative to the browser window.

27条回答
  •  天涯浪人
    2020-11-21 05:31

    Get position of div in respect to left and Top

      var elm = $('#div_id');  //get the div
      var posY_top = elm.offset().top;  //get the position from top
      var posX_left = elm.offset().left; //get the position from left 
    

提交回复
热议问题