简体   繁体   中英

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/

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM