Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4540950
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T15:09:28+00:00 2026-05-21T15:09:28+00:00

When the mouse is moved over an element, I want to get the mouse

  • 0

When the mouse is moved over an element, I want to get the mouse coordinates of the cursor relative to the top-left of the element’s content area (this is the area excluding padding, border and outline). Sounds simple, right? What I have so far is a very popular function:

function element_position(e) {
    var x = 0, y = 0;
    do {
        x += e.offsetLeft;
        y += e.offsetTop;
    } while (e = e.offsetParent);
    return { x: x, y: y };
}

And I’d get the mouse position relative to an element element with:

p = element_position(element);
x = mouseEvent.pageX - p.x;
y = mouseEvent.pageY - p.y;

That isn’t quite correct. Because the offsetLeft and offsetTop are the differences between the ‘outer’ top left of an element and the ‘inner’ top left of its offset parent, the sum position will skip over all borders and paddings in the hierarchy.

Here’s a comparison that should (hopefully) clarify what I mean.

  • If I get the sum of the distances in position between the ‘outer’ top left of the elements and the ‘inner’ top left of their offset parents (outers minus inners; what I am doing right now), I get the element’s content area’s position, minus all the borders and paddings in the offset hierarchy.
  • If I get the sum of the distances in position between the ‘outer’ top left of the elements and the ‘outer’ top left of their offset parents (outers minus outers), I get the element’s content area’s position, minus the border and padding of the desired element (close, but not quite there).
  • If I get the sum of the distances in position between the ‘inner’ top left of the elements and the ‘inner’ top left of their offset parents (inners minus inners), I get the element’s content area’s position. This is what I want.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T15:09:29+00:00Added an answer on May 21, 2026 at 3:09 pm

    Here’s a live example that uses an element_position() function that is aware of padding and borders. I’ve added some extra padding and margins to your original example.

    http://jsfiddle.net/Skz8g/4/

    To use it, move the cursor over the brown area. The resulting white area is the actual canvas content. The brown is padding, the red is a border, and so on. In both this example and the one later on, the canvas x and canvas y readouts indicate the cursor position relative to canvas content.

    Here’s the code for element_position():

    function getNumericStyleProperty(style, prop){
        return parseInt(style.getPropertyValue(prop),10) ;
    }
    
    function element_position(e) {
        var x = 0, y = 0;
        var inner = true ;
        do {
            x += e.offsetLeft;
            y += e.offsetTop;
            var style = getComputedStyle(e,null) ;
            var borderTop = getNumericStyleProperty(style,"border-top-width") ;
            var borderLeft = getNumericStyleProperty(style,"border-left-width") ;
            y += borderTop ;
            x += borderLeft ;
            if (inner){
              var paddingTop = getNumericStyleProperty(style,"padding-top") ;
              var paddingLeft = getNumericStyleProperty(style,"padding-left") ;
              y += paddingTop ;
              x += paddingLeft ;
            }
            inner = false ;
        } while (e = e.offsetParent);
        return { x: x, y: y };
    }
    

    The code should work properly in IE9, FF and Chrome, although I notice it is not quite right in Opera.

    My original inclination was to use something like the e.offsetX/Y properties because they were closer to what you want, and do not involve looping over nested elements. However, their behaviour varies wildly across browsers, so a bit of cross-browser finagling is necessary. The live example is here:

    http://jsfiddle.net/xUZAa/6/

    It should work across all modern browsers – Opera, FF, Chrome, IE9. I personally prefer it, but thought that although your original question was just about “getting mouse position relative to content area of an element”, you were really asking about how to make the element_position() function work correctly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to do this: https://github.com/rails/rails/commit/f50aeda2f73b47c47664e3651c638ba624418b8b See how, as your mouse cursor moves over
What's the best way to switch images on mouse-over events ? I want to
how can I add a class when mouse is over an element? var row
In my Direct3D application, the camera can be moved using the mouse or arrow
When mouse nears top edge of scrollpane, it scrolls up until it reaches the
I want to use the mouse scrollwheel in my OpenGL GLUT program to zoom
I want to create a keyboard and mouse hook which will be started as
Is there a way to limit mouse pointer movement to a specific area in
I want to make the drag drop to the element,and I dot not want
In Delphi 2007, in a mouse move event, I try to change the mouse

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.