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

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

    HTML elements on most browsers will have:-

    offsetLeft
    offsetTop
    

    These specifiy the position of the element relative its nearest parent that has layout. This parent can often be accessed bif the offsetParent property.

    IE and FF3 have

    clientLeft
    clientTop
    

    These properties are less common, they specify an elements position with its parents client area (padded area is part of the client area but border and margin is not).

提交回复
热议问题