EN
JavaScript - onmousemove event example
0 points
In this article, we would like to show you onmousemove
event example in JavaScript.
Quick solution:
xxxxxxxxxx
1
var myElement = document.querySelector('#element-id');
2
3
myElement.addEventListener('mousemove', function() {
4
console.log('onmousemove event occurred.');
5
});
or:
xxxxxxxxxx
1
<div onmousemove="handleMousemove()"> div body here... </div>
or:
xxxxxxxxxx
1
var myElement = document.querySelector('#element-id');
2
3
myElement.onmousemove = function() {
4
console.log('onmousemove event occurred.');
5
};
There are three common ways how to use onmousemove
event:
- with event listener,
- with element attribute,
- with element property.
In this section, we want to show how to use onmousemove
event on div
element via event listener mechanism.
xxxxxxxxxx
1
2
<html>
3
<head>
4
<style>
5
#my-div {
6
width: 200px;
7
height: 100px;
8
border: 1px solid #28a745;
9
background: #b5edc24a;
10
}
11
</style>
12
</head>
13
<body>
14
<p>Mouse over the rectangle to get the coordinates of your mouse pointer.</p>
15
<div id="my-div"></div>
16
<p id="coordinates"></p>
17
18
<script>
19
var myDiv = document.querySelector('#my-div');
20
var myDivCoordinates = document.querySelector('#coordinates');
21
22
myDiv.addEventListener('mousemove', getCoordinates); // add mousemove event
23
myDiv.addEventListener('mouseleave', clearCoordinates); // add mouseleave event
24
25
function getCoordinates(event) {
26
var x = event.clientX;
27
var y = event.clientY;
28
var coordinates = 'x = ' + x + ', y = ' + y;
29
myDivCoordinates.innerHTML = coordinates;
30
}
31
32
function clearCoordinates() {
33
myDivCoordinates.innerHTML = "";
34
}
35
</script>
36
</body>
37
</html>
In this section, we want to show how to use onmousemove
event on div
element via attribute.
xxxxxxxxxx
1
2
<html>
3
<head>
4
<style>
5
#my-div {
6
width: 200px;
7
height: 100px;
8
border: 1px solid #28a745;
9
background: #b5edc24a;
10
}
11
</style>
12
</head>
13
<body>
14
<p>Mouse over the rectangle to get the coordinates of your mouse pointer.</p>
15
<div id="my-div" onmousemove="getCoordinates(event)" onmouseout="clearCoordinates()"></div>
16
<p id="coordinates"></p>
17
18
<script>
19
var myDivCoordinates = document.querySelector('#coordinates');
20
21
function getCoordinates(event) {
22
var x = event.clientX;
23
var y = event.clientY;
24
var coordinates = 'x = ' + x + ', y = ' + y;
25
myDivCoordinates.innerHTML = coordinates;
26
}
27
28
function clearCoordinates() {
29
myDivCoordinates.innerHTML = "";
30
}
31
</script>
32
</body>
33
</html>
In this section, we want to show how to use onmousemove
event on div
element via property.
xxxxxxxxxx
1
2
<html>
3
<head>
4
<style>
5
#my-div {
6
width: 200px;
7
height: 100px;
8
border: 1px solid #28a745;
9
background: #b5edc24a;
10
}
11
</style>
12
</head>
13
<body>
14
<p>Mouse over the rectangle to get the coordinates of your mouse pointer.</p>
15
<div id="my-div"></div>
16
<p id="coordinates"></p>
17
18
<script>
19
var myDiv = document.querySelector('#my-div');
20
var myDivCoordinates = document.querySelector('#coordinates');
21
22
myDiv.onmousemove = function(event) {
23
var x = event.clientX;
24
var y = event.clientY;
25
var coordinates = 'x = ' + x + ', y = ' + y;
26
myDivCoordinates.innerHTML = coordinates;
27
}
28
29
myDiv.onmouseleave = function() {
30
myDivCoordinates.innerHTML = "";
31
}
32
</script>
33
</body>
34
</html>