onmouseover - How to calculate the mouse position from the center of an element in Jquery -


i'm trying find way of calculating mouse position center of element.

i've got far e.pagex , e.pagey on mouse on event, can't head around calculating position relative elements center.

i can't think of equation

you need first object's center point, x & y (objcenterx & objcentery in code, below), subtract mouse's current coordinates.

this should you:

    $("#test").mousemove(function (event) {         var objleft = $("#test").offset().left;         var objtop = $("#test").offset().top;          var objcenterx = objleft + $("#test").width() / 2;         var objcentery = objtop + $("#test").height() / 2;          $("#results").text("left:" + (event.pagex - objcenterx) + ", top:" + (event.pagey - objcentery));     }) 

Comments