1

So I have a div element that gets animated when you hover over it. I want to detect when the div element leaves the point 0, 0, and when it returns to 0, 0. I have the base code already, I just don't know the variables I would plug in.

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")

And to finish off, I am just starting HTML and JavaScript, so if this is a super obvious solution, then I guess I'll have to go do more reasearch.

Cache Staheli
  • 3,510
  • 7
  • 32
  • 51
Justin Jung
  • 27
  • 2
  • 8
  • can you post the entire code? – Nico Feb 02 '17 at 22:56
  • 1
    Check this https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect – iamkd Feb 02 '17 at 22:59
  • 1
    Possible duplicate of [Retrieve the position (X,Y) of an HTML element](http://stackoverflow.com/questions/442404/retrieve-the-position-x-y-of-an-html-element) – Veve Feb 02 '17 at 23:02

1 Answers1

1

You can use getBoundingClientRect()

var rect = obj.getBoundingClientRect();

rect is an object with eight properties: left, top, right, bottom, x, y, width, height

source from MDN

Fady Sadek
  • 1,091
  • 1
  • 13
  • 22