如何在屏幕上找到div元素的X和Y坐标?

时间:2017-02-02 22:46:27

标签: javascript html

所以我有一个div元素,当你将鼠标悬停在它上面时会生成动画。我想检测div元素何时离开点0, 0,以及何时返回0, 0。我已经有了基本代码,我只是不知道我要插入的变量。

var divx = getElementById("div").position.x;
var divy = getElementById("div").position.y;
if (divx === 0 && divy === 0) {
    window.alert("Div has returned to starting position")

最后,我只是开始使用HTML和JavaScript,所以如果这是一个非常明显的解决方案,那么我想我将不得不做更多的研究。

1 个答案:

答案 0 :(得分:1)

您可以使用getBoundingClientRect()

var rect = obj.getBoundingClientRect();

rect是一个具有八个属性的对象:left,top,right,bottom,x,y,width,height

Using a simple vue.js/webpack setup, how does one configure the dev server to proxy everything EXCEPT a few .js and .vue files?