Friday, August 28, 2009

How to get Mouse Cursor Position

There is no Certain way to find the Cursor position of mouse within the Browser.
W3c standards say that you can get the position when event is triggered by event.clientX and same for clientY. to find out the Distance between left n Top


Its works fine on IE6 , Netsacape , Firefox , Opera 7+ and all othe browser.
in IE uses two Different ways to scroll the position of pages they are using Document.body.ScrollLeft and same for the ScrollTop. this is in Before version

of 6
Some of the Browser used PageXOffset & PageYOffset.

Here is Function call MouseX() and MouseY() which can calculate the X and Y Co-ordinates of same,

function mXbar(action) {
if (action.pageX) return action.pageX;
else if (action.clientX)
return action.clientX + (document.documentElement.scrollLeft ?document.documentElement.scrollLeft :document.body.scrollLeft);
else return null;
function mYbar(action) {
if (action.pageY) return action.pageY;
else if (action.clientY)
return action.clientY + (document.documentElement.scrollTop ?document.documentElement.scrollTop :document.body.scrollTop);
else return null;

there are few more ways to calculate the cursor position event.ScrenX or event.screenY
these feature are not thee in Netscape4 and IE old version, but in new version of ie 7 + they have all such feature,

No comments:

Post a Comment