2009-12-01 9 views

Répondre

1

Voici la méthode que j'utilise lorsque je veux obtenir la position de la souris dans un élément. Il renvoie la valeur et dans un axe standard (bas -> haut) ou dans l'axe de la bande (haut -> bas).

/* 
    Get the position of the mouse event in a standard axis system 
    in relation to the given element. 

    Standard axis system: 
    The origin (0, 0) starts at the bottom-left and increases 
    going up for 'y' and right for 'x'. 
*/ 
function GetMousePositionInElement(ev, element) 
{ 
    var offset = element.offset(); 

    var bottom = offset.top + element.height(); 

    var x = ev.pageX - offset.left; 
    var y = bottom - ev.pageY; 

    return { x: x, y: y, y_fromTop: element.height() - y }; 
} 

Il nécessite jQuery.

Questions connexes