Jump to content

Get Mouse Position.


MrFish

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);

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...