Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / HTML, CSS, Scripts / JavaScript / June 2006



Tip: Looking for answers? Try searching our database.

mouse click location

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
quickcur@yahoo.com - 29 Jun 2006 23:30 GMT
Hi, I have html like this:

<div id="myCanvas" style="border:10px,
black;position:relative;height:250px;width:100%;">
<img id="p" src="p.jpg">
</div>

When user click the mosue, I would like

1. Test if the mouse location is on top of the image
2. Find the relative mouse location respect to the top-left corner of
the image.
3. If all yes, do something.

I am using event.x and event.y but I do not know the x and y is
relative location or what?

Thanks,

qq
Jim - 30 Jun 2006 05:08 GMT
Quick Cur,
Here's a MSIE and FireFox compatible version of the msie-only script i
sent you privately:
As you'll see, the mouse coordinates are posted in the status bar; when
they broach the inside of the red-dashed div box, the script will
trigger and re-locate the page to yahoo.com; for your needs, the
coordinates can equal the top left corner of your image, if it is
absolutely positioned on the page.  (ie: you may have to detect the
image position if it is dynamically placed or relative to other
components).   HTH  - Jim
----------------------------------
<html>
<head>
<title>mouseTracker and event trigger based on mouse
coordinates</title>
<script language="javascript">
function mCo(evt ){
// create cross-browser event detector:
var node = (evt.target) ? evt.target : ((evt.srcElement)
?evt.srcElement : null );
evt = (evt) ? evt : ((event) ? event  :  null);

// create mouse coordinates objects:
xpo = evt.clientX;
ypo = evt.clientY;

// Set left-right, top-bottom params within 'if' stmt to equal
//  the bounded box (or image) you want to detect:
if( (xpo >= 100) && (xpo <= 200 ) && (ypo >= 100) && (ypo <= 200) ){
  window.location = "http://www.yahoo.com";
   }

// post all coordinates to the status bar while the mouse is moving:
window.status= "X-coordinate is:  " + xpo + "  Y-coordinate  is:  " +
ypo ;
}
</script>
</head>
<body bgcolor="black" color="white" onMousemove="mCo(event)">
<div style=" background-color:teal; color:white; border:3px gray ridge;
width:300px;
height:50px; z-index:1"  id='the_box'>
Below is a red target area.  Your mouse position will be tracked (see
the mouse coordinates in the status bar area) so that  if your mouse
moves into the 'target' area, the window.location will change.
</div>
<div style="position:absolute; left:100; top:100;
background-color:transparent; border:1px red dashed; width:100px;
height:100px; z-index:2"  id='targetarea'>&nbsp;
</div>
</body>
</html>
----------------------------------
RobG - 30 Jun 2006 06:52 GMT
> Quick Cur,
> Here's a MSIE and FireFox compatible version of the msie-only script i
> sent you privately:

Please don't top post, it destroys the flow of the thread.  Your script
and explanations are very ordinary, the OP will get a much better idea
of events and their properties by reading quirksmode:

 Event properties, including location:
 <URL:http://www.quirksmode.org/js/events_properties.html>

 'mouse' events, including mouse move, over elements, etc.
 <URL:http://www.quirksmode.org/js/events_mouse.html>

For the record, quirksmode suggests getting cursor coordinates using:

function doSomething(e)
{
  var posx = 0;
  var posy = 0;
  if (!e) var e = window.event;
  if (e.pageX || e.pageY)
    {
      posx = e.pageX;
      posy = e.pageY;
    }
      else if (e.clientX || e.clientY)
    {
      posx = e.clientX + document.body.scrollLeft;
      posy = e.clientY + document.body.scrollTop;
    }
  // posx and posy contain the mouse position relative to the document
  // Do something with this information
}

[...]

Signature

Rob

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.