Krab
Krab

Reputation: 6756

Hover event on div under absolute positioned div

Is possible to obtain hover event on element, which is under absolute positioned div? That absolute positioned div is child of body element, but that under isn't, so they are not in relationship parent/child. I do drag and drop of that absolute div and i want highlight area, where user can drop, when mouse it's under that area.

enter code here

http://jsfiddle.net/Rv8kp/

Upvotes: 1

Views: 1379

Answers (1)

maxlego
maxlego

Reputation: 4914

Short answer would be no, you cant. But.. there is a workaround

You can add mousemove event handler for the whole document. Inside the handler you check if mouse position is in the area of the element you need to hover.

var $pos = $("#pos");
var top = $pos.offset().top;
var left = $pos.offset().left;
var bottom = top + $pos.height();
var right = left + $pos.width();

$(document).mousemove(function (e) {

    if (e.pageY >= top && e.pageY <= bottom && e.pageX >= left && e.pageX <= right)
        $pos.addClass("hover");
    else
        $pos.removeClass("hover");
});

you can view full working example here

Upvotes: 2

Related Questions