Jump to content

Get Mouse Position.


MrFish
 Share

Recommended Posts

I've made an ajax script that when you hover the mouse over an object it fetches the data and inserts it into a div that has a z-index above the rest of the page. I'm trying to make it so it's x and y coordinates match the cursors so it's looks kind of like a tool tip but follows your cursor around. I've tried window.event.clientX and it's partner but it won't work-

	function reposition(){		if(showinfo.visibility == "visible"){			mleft = window.event.clientX + "px";			mtop = window.event.clientY + "px";			showinfo.top = mtop;			showinfo.left = mleft;			setTimeout("reposition()", 50);		}	}

Can anyone tell me of something that actually works?

Link to comment
Share on other sites

I've made an ajax script that when you hover the mouse over an object it fetches the data and inserts it into a div that has a z-index above the rest of the page. I'm trying to make it so it's x and y coordinates match the cursors so it's looks kind of like a tool tip but follows your cursor around. I've tried window.event.clientX and it's partner but it won't work-
	function reposition(){		if(showinfo.visibility == "visible"){			mleft = window.event.clientX + "px";			mtop = window.event.clientY + "px";			showinfo.top = mtop;			showinfo.left = mleft;			setTimeout("reposition()", 50);		}	}

Can anyone tell me of something that actually works?

That's not going to work in most browsers. window.event is exclusive to Internet Explorer. First you must learn how to handle events:http://www.quirksmode.org/js/introevents.htmlThis page explains how to get the mouse position from an event: http://www.quirksmode.org/js/events_properties.html#positionBy the way, for more efficiency, don't use expressions in the setTimeout function, put a reference to the function instead, it's much faster:
setTimeout(reposition, 50);

Edited by Ingolme
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...