3

I'm creating a drag and drop application and what I want to do is while the user is dragging an element get if the cursor is over certain other element.

Heres the flow:

User starts dragging an element around the page
When the mouse gets over certain parts I want to make an ajax call.

Because I know the elements and their bounds via jquery I just want the best way to make the check.

locrizak
  • 12,192
  • 12
  • 60
  • 80

3 Answers3

3

The snippet mentioned by

https://stackoverflow.com/users/650094/adam-terlson

from

find elements that are stacked under (visually) an element in jquery

looks like this:

function mouseWithin(bounds,x,y) {
    var offset = bounds.offset();
    var l = offset.left;
    var t = offset.top;
    var h = bounds.height();
    var w = bounds.width();

    var maxx = l + w;
    var maxy = t + h;

    return (y <= maxy && y >= t) && (x <= maxx && x >= l);
};

you can use it like this:

var gallery=$("#carousel-gallery");
mouseWithin(gallery,event.pageX,event.pageY);
Community
  • 1
  • 1
Wolfgang Fahl
  • 15,016
  • 11
  • 93
  • 186
0

$("#element").hover();

Hover

Eric Frick
  • 847
  • 1
  • 7
  • 18
-1

Start here: http://docs.jquery.com/Tutorials:Mouse_Position

Get the mouse coordinates and compare it to the .offset() values of the item you're comparing it to.

http://api.jquery.com/offset/

Adam Terlson
  • 12,610
  • 4
  • 42
  • 63
  • Thats what I'm doing, but i was wondering if there is a better/easier way to do it – locrizak Apr 20 '11 at 18:55
  • Generally it's safe to use events, but since you're bringing an item with you that's overlapping, the answer is: probably not. :( – Adam Terlson Apr 20 '11 at 18:57
  • Maybe hijack some of the code from this answer: http://stackoverflow.com/questions/5598953/find-elements-that-are-stacked-under-visually-an-element-in-jquery/5599301#5599301 It might be of value. – Adam Terlson Apr 20 '11 at 18:59