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

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

    You might be better served by using a JavaScript framework, that has functions to return such information (and so much more!) in a browser-independant fashion. Here are a few:

    • Prototype
    • jQuery
    • MooTools
    • YUI (yahoo)

    With these frameworks, you could do something like: $('id-of-img').top to get the y-pixel coordinate of the image.

提交回复
热议问题