Before reading this article, please go through
the following articles:
Introduction
In this article you
will learn about mouse events. There are seven types of mouse events, they are
- Onclick
- Ondblclick
- Onmousedoen
- Onmousemove
- Onmouseover
- Onmouseout
- Onmouseup
I have divided these
mouse events into three parts.
Third part
Today you will learn
about the OnMouseMove, OnMouseDown and OnMouseUp events.
OnMouseMove
The
mousemove
the event works fine, but you should be aware that it may take quite some system
time to process all mousemove events. If the user moves the mouse one pixel then
the
mousemove
event fires. Even when nothing actually happens, long and complicated functions
take time and this may affect the usability of the site: everything goes very
slowly, especially on old computers.
Therefore it's best to
register an
onmousemove
event handler only when you need it and to remove it as soon as it's not needed anymore. The following is an example.
Example
- <!DOCTYPE html>
- <html>
- <head>
- <style>
- div
- {
- width: 199px;
- height: 99px;
- border: 1px solid;
- margin: 45px;
- }
- </style>
- <script>
- function myFunction(event) {
- x = event.clientX;
- y = event.clientY;
- coor = "result: (Yaxis-" + x + "Xaxis-" + y + ")";
- document.getElementById("demo").innerHTML = coor
- }
- function clearCoor() {
- document.getElementById("demo").innerHTML = "";
- }
- </script>
- </head>
- <body style="margin: 0px;">
- <div id="coor" onmousemove="myFunction(event)" onmouseout="clearCoor()">
- <p id="demo">
- </p>
- </div>
- <p>
- Mouse over the rectangle above, and get the coordinates of your mouse pointer.</p>
- </body>
- </html>



Join the conversation! Your thoughts help the community grow.