Click here to Skip to main content
15,884,960 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Been stuck on this for a while. I can find the cursor position on a mouse move like this:

JavaScript
function myMoveFunction()
{
   document.getElementById("ImageCaption").innerHTML = "Coords: " + event.x + ", " + event.y ;
}


ASP.NET
<asp:ImageButton id="imagebutton1" runat="server" Width="512" Height="262"
                    ImageUrl="DrawRoom.aspx?v=0"
                    OnClick="ImageButton_Click"
                    onmousemove="myMoveFunction()"  />
<p><span id="ImageCaption"></span></p>


And this gives me the position relative to the window very handily, but I need to subtract the position of the image. I've found lots of code which returns me blankness.

JavaScript
function FindPosition(oElement) {
    if (typeof (oElement.offsetParent) != "undefined") {
        for (var posX = 0, posY = 0; oElement; oElement = oElement.offsetParent) {
            posX += oElement.offsetLeft;
            posY += oElement.offsetTop;
        }
        return [posX, posY];
    }
    else {
        return [oElement.x, oElement.y];
    }
}


JavaScript
function myMoveFunction()
{
   var pos = FindPosition(document.getElementbyID("imagebutton1"));
   document.getElementById("ImageCaption").innerHTML = "Coords: " + pos[0]+ ", " + pos[1] ;
}


Gives me only blank in my imagecaption
Posted

1 solution

What would you expect? In your method FindPosition, you are not even trying to do anything related to the mouse.

You can get mouse coordinates from the event argument of the event itself. The most convenient way of doing it would be using jQuery: http://api.jquery.com/mousemove[^].

If you need to learn jQuery (highly recommended), please see:
http://en.wikipedia.org/wiki/JQuery[^],
http://jquery.com[^],
http://learn.jquery.com[^],
http://learn.jquery.com/using-jquery-core[^],
http://learn.jquery.com/about-jquery/how-jquery-works[^] (start from here).

To do it without any library, please use the event object and event properties clientX and clientY:
http://javascript.info/tutorial/mouse-events#mouse-coordinates-clientx-y-pagex-y[^].

—SA
 
Share this answer
 
v2
Comments
Troublesome Tommy 13-Oct-14 19:00pm    
I already have the mouse position from the event. I just need to find the position of the stupid control.
Sergey Alexandrovich Kryukov 13-Oct-14 19:35pm    
Find? What do you mean by "finding"?
—SA
Troublesome Tommy 14-Oct-14 9:31am    
I can see this is going to be real helpful.
Sergey Alexandrovich Kryukov 14-Oct-14 12:11pm    
I'm sure you will solve it. The method I mentioned will give you the mouse pointer location on client coordinates.
Please use it and accept the answer formally (green "Accept" button).
In all cases, your follow-up questions will be welcome.
—SA
Troublesome Tommy 14-Oct-14 13:07pm    
Well, the consensus on jquery is "Don't try to live without it."

So, to get this stupid little app working before I dive in to learn a whole new library, I now have

(No code in a "Comment?")


function getOffset(el) {
var _x = 0;
var _y = 0;
while (el && !isNaN(el.offsetLeft) && !isNaN(el.offsetTop)) {
_x += el.offsetLeft - el.scrollLeft;
_y += el.offsetTop - el.scrollTop;
el = el.offsetParent;
}
return { top: _y, left: _x };
}

function myMoveFunction() {
var x = getOffset(document.getElementById('TheDiv')).left;
var y = 7;//getOffset(document.getElementByID('TheDiv')).top;
document.getElementById("demo").innerHTML = event.x - x;
document.getElementById("y2").innerHTML = event.y-y;
}



But if I change y to the getoffset line instead of 7, the myMoveFunction doesn't do anything. See it at http://thinktulsa17.com/vendorhall/mousetest.html

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900