Getting the location of an event and accounting for scrolling

60 views
Skip to first unread message

e-z-5M

unread,
Oct 3, 2006, 6:01:57 PM10/3/06
to Google Web Toolkit
GWT's DOM class has the

eventGetClientX(Event) Gets the mouse x-position within the browser
window's client area.
eventGetClientY(Event) Gets the mouse y-position within the browser
window's client area.

and

eventGetScreenX(Event) Gets the mouse x-position on the user's display.

eventGetScreenY(Event) Gets the mouse y-position on the user's display.


methods, but I couldn't find similar ones that give the location
(clientX/Y) taking into account the effect of scrolling. If you know
of some that I've overlooked, please point them out. If you too have
needed these and couldn't find them, here's a utility class:

/**
* A non-instantiable class with static DOM utility methods.
*
* @author epstein
*/
public abstract class DOMUtil {

/**
* Get the XY co-ordinates of an event. Similar to
DOM.eventGetClientX() and DOM.eventGetClientY()
* except it corrects of scrolling of the browser window.
*
* @param e a mouse event
* @return A JavaScript int array (int[]) wrapped in an opaque
JavaScriptObject handle.
*/
public static native JavaScriptObject eventGetXYPosition(Event e) /*-{
var scrOfX = 0;
var scrOfY = 0;
if ( typeof( $wnd.pageXOffset ) == 'number' || typeof(
$wnd.pageYOffset ) == 'number' ) {
//Netscape compliant
scrOfX = $wnd.pageXOffset;
scrOfY = $doc.documentElement.scrollTop;
} else if ( $doc.body && ( $doc.body.scrollLeft ||
$doc.body.scrollTop ) ) {
//DOM compliant
scrOfX = $doc.body.scrollLeft;
scrOfY = $doc.body.scrollTop;
} else if ( $doc.documentElement && ( $doc.documentElement.scrollLeft
|| $doc.documentElement.scrollTop ) ) {
//IE6 standards compliant mode
scrOfX = $doc.documentElement.scrollLeft;
scrOfY = $wnd.pageYOffset;
}
return [scrOfX + e.clientX, scrOfY + e.clientY];
}-*/;

public static native int getIntAtIndex(JavaScriptObject intArray, int
idx) /*-{
return intArray[idx];
}-*/;

public static int eventGetXPosition(Event e) {
return getIntAtIndex(eventGetXYPosition(e), 0);
};

public static int eventGetYPosition(Event e) {
return getIntAtIndex(eventGetXYPosition(e), 1);
};

}

Reply all
Reply to author
Forward
0 new messages