9

Basicly I want to check if one div is hovering another div, I've searched a lot for this, but couldn't find anything D:

I made a div that you can control by using the arrow keys, so now I want to check if that div is hovering another div, if so it should do something.

I really can't figure this out, please help.

Mobilpadde
  • 1,871
  • 3
  • 22
  • 29
  • 2
    Tried doing some math and using .offset ? [http://api.jquery.com/offset/](http://api.jquery.com/offset/) -Something like calculating the position of the moving DIV, that if offset is = or > than the offset of your DIV.... do something. – Roko C. Buljan May 15 '11 at 13:10
  • @roXon But would that work on any screen resolution? Because, I've made it with % – Mobilpadde May 15 '11 at 13:20
  • it should work 'cause offset is dynamic and is based on a current window (or DOM) size, calculating some element offset (Ex: top and left) in px. – Roko C. Buljan May 15 '11 at 13:22
  • Ohh, but would be nice if there was some easier way to do it – Mobilpadde May 15 '11 at 13:26
  • @Moiblpadde if you just want to achieve the 'DO SOMETHING' it's fine, 'cause either if your fixed (or non-fixed) element is positioned in % jQuery will count the element offset in px. respective to the elem. position inside the document. Give it a go! :) I think this could be fun! – Roko C. Buljan May 15 '11 at 13:26
  • @Moiblpadde something like: get the width() and height() of your element, calculate the offset and you'll get his position and 'colliding' size in the document! – Roko C. Buljan May 15 '11 at 13:43
  • It's kinda work, the only problem is, if the browser isn't high enough, it's not working, because The div I'm hovering is in the bottom-left corner, so if, I do the browser smaller, the offset top never be 830 or more (Srry for my english) – Mobilpadde May 15 '11 at 13:56
  • you have tried to set the counting offsets to some internal (container) div (not to the screen size) – Roko C. Buljan May 15 '11 at 14:03
  • I encountered this problem, and it got me here, after awhile I discovered that you could tweak this easily with DRAG and DROP callbacks :) It works pretty well for me. – Roy Lee Aug 10 '12 at 09:37
  • Hmm, haven't tried that... But anyway, got it to work, by using some offset math – Mobilpadde Aug 10 '12 at 13:09

2 Answers2

2

You may take a look at hitTestObject

Dr.Molle
  • 116,463
  • 16
  • 195
  • 201
0

It seems that you want to detect collision between two elements. So, on some event (keyDown maybe in your case) you have to detect if any of corners of first element (top-left, top-right, bottom-left, bottom-right) is within second element area (somewhere between his corners).

Ivan Lazarevic
  • 371
  • 2
  • 7