Javascript real-time acquisition and display of mouse coordinate values

Source: Internet
Author: User

Javascript real-time acquisition and display of mouse coordinate values

This article mainly introduces how to obtain and display the mouse coordinate values in javascript in real time. It involves the skills related to javascript mouse events and is very useful. For more information, see

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN"

Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd>

<Html xmlns = "http://www.w3.org/1999/xhtml">

<Head>

<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>

<Title> JS obtains and displays mouse coordinate values in real time </title>

</Head>

<Body>

<Script type = "text/javascript">

Var getcoordin1_entexample = function (){

Var coords = document. getElementById ("coords ");

Coords. onmousemove = function (e ){

Var pointer = getCoordInDocument (e );

Var coord = document. getElementById ("coord ");

Coord. innerHTML = "X, Y = (" + pointer. x + "," + pointer. y + ")";

}

}

Var getCoordInDocument = function (e ){

E = e | window. event;

Var x = e. pageX | (e. clientX +

(Document.doc umentElement. scrollLeft

| Document. body. scrollLeft ));

Var y = e. pageY | (e. clientY +

(Document.doc umentElement. scrollTop

| Document. body. scrollTop ));

Return {'X': x, 'y': y };

}

Window. onload = function (){

Getcoordin1_entexample ();

};

</Script>

<Div id = "coords"

Style = "width: 500px; height: 200px; background: # F2F1D7;

Border: 2px solid # 0066cc; ">

Move the mouse here.

</Div>

<Br/>

<Div id = "coord"

Style = "width: 500px; border: 2px solid #336699;"> </div>

</Body>

</Html>

I hope this article will help you design javascript programs.

Related Article

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.