EN
JavaScript - onmouseout event example
0 points
In this article, we would like to show you onmouseout
event example in JavaScript.
Quick solution:
xxxxxxxxxx
1
var myElement = document.querySelector('#my-element');
2
3
myElement.addEventListener('mouseout', function() {
4
console.log('onmouseout event occurred.');
5
});
or:
xxxxxxxxxx
1
<div onmouseout="handleMouseout()"> div body here... </div>
or:
xxxxxxxxxx
1
var myElement = document.querySelector('#my-element');
2
3
myElement.onmouseout = function() {
4
console.log('onmouseout event occurred.');
5
};
Note:
The
onmouseout
is usually used withonmouseover
event. Check examples below to see how they work.
There are three common ways how to use onmouseout
event:
- with event listener,
- with element attribute,
- with element property.
In this section, we want to show how to use onmouseout
event on div
element via event listener mechanism.
xxxxxxxxxx
1
2
<html>
3
<head>
4
<style>
5
body {
6
padding: 0 0 0 10px;
7
height: 260px;
8
}
9
#square {
10
padding: 10px;
11
height: 120px;
12
width: 120px;
13
border-radius: 10px;
14
background: #b5edc2;
15
transition: 1s;
16
}
17
</style>
18
</head>
19
<body>
20
<p>Move mouse pointer onto the square:</p>
21
<div id="square"/>
22
<script>
23
var mySquare = document.querySelector('#square'); // find element by id
24
25
mySquare.addEventListener("mouseover", handleMouseover); // add mouseover event
26
27
function handleMouseover(){
28
mySquare.style.height = "200px";
29
mySquare.style.width = "200px";
30
}
31
32
mySquare.addEventListener("mouseout", handleMouseout); // add mouseout event
33
34
function handleMouseout(){
35
mySquare.style.height = "120px";
36
mySquare.style.width = "120px";
37
}
38
</script>
39
</body>
40
</html>
In this section, we want to show how to use onmouseout
event on div
element via attribute.
xxxxxxxxxx
1
2
<html>
3
<head>
4
<style>
5
body {
6
padding: 0 0 0 10px;
7
height: 260px;
8
}
9
#square {
10
padding: 10px;
11
height: 120px;
12
width: 120px;
13
border-radius: 10px;
14
background: #b5edc2;
15
transition: 1s;
16
}
17
</style>
18
</head>
19
<body>
20
<p>Move mouse pointer onto the square:</p>
21
<div id="square" onmouseover="handleMouseover()" onmouseout="handleMouseout()"/>
22
<script>
23
var mySquare = document.querySelector('#square');
24
25
function handleMouseover(){
26
mySquare.style.height = "200px";
27
mySquare.style.width = "200px";
28
}
29
30
function handleMouseout(){
31
mySquare.style.height = "120px";
32
mySquare.style.width = "120px";
33
}
34
</script>
35
</body>
36
</html>
In this section, we want to show how to use onmouseout
event on div
element via property.
xxxxxxxxxx
1
2
<html>
3
<head>
4
<style>
5
body {
6
padding: 0 0 0 10px;
7
height: 260px;
8
}
9
#square {
10
padding: 10px;
11
height: 120px;
12
width: 120px;
13
border-radius: 10px;
14
background: #b5edc2;
15
transition: 1s;
16
}
17
</style>
18
</head>
19
<body>
20
<p>Move mouse pointer onto the square:</p>
21
<div id="square"/>
22
<script>
23
var mySquare = document.querySelector('#square');
24
25
mySquare.onmouseover = function(){
26
mySquare.style.height = "200px";
27
mySquare.style.width = "200px";
28
}
29
30
mySquare.onmouseout = function(){
31
mySquare.style.height = "120px";
32
mySquare.style.width = "120px";
33
}
34
</script>
35
</body>
36
</html>