Use javascript to obtain the code _ javascript technique for the cursor position on the page and the object that triggers the event

Source: Internet
Author: User
Use javascript to obtain the cursor position on the page and the object that triggers the event. use javascript to obtain the cursor position:

The Code is as follows:


Function mousePosition (ev ){
If (ev. pageX | ev. pageY ){
Return {
X: ev. pageX,

Y: ev. pageY
};
}
Return {
X: ev. clientX + document. body. scrollLeft-document. body. clientLeft,

Y: ev. clientY + document. body. scrollTop-document. body. clientTop
};
}

Document. onmousemove = mouseMove;
Function mouseMove (ev ){
Ev = ev | window. event;
Var mousePos = mousePosition (ev );
}


For a detailed description of the Code, the original article has already been introduced and is now transferred here:
First, we need to declare an evnet object. An evnet object will be activated no matter whether it is moved, clicked, or pressed. in Internet Explorer, event is a global variable and will be stored in window. event. in firefox or other browsers, events are obtained by corresponding functions. when we assign the mouseMove function to document. onmousemove, mouseMove will get the mouse movement event.
To allow ev to obtain event Events in all browsers, "| window. event" in Firefox does not work because ev has a value assignment. Ev is empty in MSIE, so the window. event is obtained.
Because we need to obtain the mouse position multiple times in this article, we have designed a mousePosition function, which contains a parameter: event.
Because we want to run in MSIE and other browsers, Firefox and other browsers use event. pageX and event. pageY indicates the position of the mouse relative to the document. If you have a 500*500 window and your mouse is in the absolute middle, the pageX and pageY values are both 250, if you scroll down 500, pageY will become 750.
The opposite is true for MSIE. It uses event. clientX and event. clientY to indicate the location where the mouse is equivalent to the window, rather than the document. In the same example, If you scroll down to 500, clientY is still 250. Therefore, we need to add the attributes of scrollLeft and scrollTop relative to the document. Finally, the document in MSIE does not start from 0 to 0, but usually has a small border (usually 2 pixels). The border size is defined in document. body. in clientLeft and clientTop, we also add these.
Fortunately, we have used the mousePosition function to solve the coordinate problem, so we don't have to worry about it.

Use javascript to obtain the object that triggers the event

The Code is as follows:


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.