Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Calculate relative mouse coordinates
Old 02-03-2010, 09:38 AM Calculate relative mouse coordinates
css
Novice Talker

Posts: 6
Trades: 0
It seems the border is not taken into account in the JS offsetTop/Left properties, in FF 3.6. Excerpts

Code:
<div id="wall">
     <div id="small" class="zimg">
       <img src="..." 300x200/>
     </div>
     <br style="clear:both />
</div>
Some CSS
Code:
.zimg {
   border:20px solid #ddd;
    background:#eee;
   overflow:hidden;
    position:relative;
}

#wall {
  margin:50px 0 0 50px;
}


#small {
  width:300px;
  height:200px;
  float:left;
}
And a JS function to calculate the relative (x,y) coordinates of the mouse over the IMG
Code:
function objabs(o) 
{
   var x = 0,y = 0;

    console.log("get_abs: of "+o.id);
   while (o) {
        console.log("  "+o.id+" x("+o.offsetLeft+") y("+o.offsetTop+")   P:"+(o.offsetParent ? o.offsetParent.id : o.offsetParent));
      x += o.offsetLeft;
      y += o.offsetTop;
      o = o.offsetParent;
   }
   return [x,y];
}
Result: mouse (x,y) are always off by the size of the border (20px).
(console.log() is from Firebug to get a proof!:
smpic x(0) y(0) P:small
small x(50) y(50) P:
x(0) y(0) P:null

50px is the '#wall' margin, border is nowhere.

The border is not taken into account in the calculation - is it a bug (FF or mine)?
Thanks

nb: sorry for the double post but I cannot delete the thread in css - and the JS is probably more appropriate.

Last edited by css; 02-03-2010 at 09:40 AM..
css is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 02-03-2010, 10:49 AM Re: Calculate relative mouse coordinates
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,383
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Previous Thread
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 02-05-2010, 08:42 AM Re: Calculate relative mouse coordinates
css
Novice Talker

Posts: 6
Trades: 0
Answer: it is a bug in Firefox, and the function above works as expected in other browsers (Opera...).

So, thanks to the answers on the Css thread, and don't forget to revise your box model ;-)
css is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Calculate relative mouse coordinates
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.32587 seconds with 12 queries