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

前端 未结 27 4415
闹比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:46

    if using jQuery, the dimensions plugin is excellent and allows you specify exactly what you want.

    e.g.

    Relative position, absolute position, absolute position without padding, with padding...

    It goes on, let's just say there is a lot you can do with it.

    Plus the bonus of using jQuery is it's lightweight file size and easy use, you won't go back to JavaScript without it afterwards.

提交回复
热议问题