How to grab the x, y position of html elements with javascript

后端 未结 3 2142
臣服心动
臣服心动 2021-02-15 17:08

I have in my html document several div elements with certain css class and want to get x, y position of those elements, thanks in advance.

3条回答
  •  既然无缘
    2021-02-15 17:31

    The examples bellow show how to retrieve the ClientRect of an HTML Element

    # first tag link of this page
    document.getElementsByClassName('post-taglist')[0].children[0].getClientRects()[0]
    # question div
    document.getElementById('question').getClientRects()[0]
    

    With it you have acces to right, top, height, width, left and bottom attributes.

提交回复
热议问题