HTML DOM MouseEvent Object

The HTML DOM MouseEvent Object represents an event which incurs on interaction of mouse with the HTML document elements.
Here, “MouseEvent” can have the following properties and methods −
Let us see an example of MouseEvent clientX property −
 Live Demo
<!DOCTYPE html>
<html>
<head>
<title>MouseEvent clientX</title>
<style>
   * {
      padding: 2px;
      margin:5px;
   }
   form {
      width:70%;
      margin: 0 auto;
      text-align: center;
   }
   #outer {
      width:70%;
      margin: 0 auto;
      padding: 0;
      text-align: center;
      border:1px solid black;
      height: 105px;
      background-color: #28a745;
   }
   input[type="button"] {
      border-radius: 10px;
   }
   #upper {
      border-bottom: 1px solid black;
      height: 40px;
      margin: 0 0 15px 0;
      background-color: #DC3545;
   }
   #lower {
      border-top: 1px solid black;
      height: 40px;
      margin: 15px 0 0 0;
      background-color: #DC3545;
   }
</style>
</head>
<body>
   <form>
      <fieldset>
         <legend>MouseEvent-clientX</legend>
         <div id="outer">
         <div id="upper"><h2>Danger</h2></div>
         <div id="lower"><h2>Danger</h2></div>
         </div>
         <input type="button" id="start" value="Start" onclick="gameStart()">
         <div id="divDisplay"></div>
      </fieldset>
   </form>
<script>
   var divDisplay = document.getElementById('divDisplay');
   var gameDisplay = document.getElementById('outer');
   function playGame(event) {
      var x = event.clientX;
      var y = event.clientY;
         if(y > 95 &amp;&amp; y < 110){
            divDisplay.textContent = 'Keep Going!';
               if(x === 439){
                  divDisplay.textContent = 'Congrats! You Did it!';
                  gameDisplay.removeEventListener('mousemove', playGame);
               }
         }
         else{
               divDisplay.textContent = 'You moved to DANGER area. You loose!';
               gameDisplay.removeEventListener('mousemove', playGame);
         }        
   }
   function gameStart(){
      gameDisplay.addEventListener('mousemove',playGame);
   }
</script>
</body>
</html>
After clicking ‘Start’ button and cursor in green (safe) area −
After clicking ‘Start’ button and cursor at end of green (safe) area −
After clicking ‘Start’ button and cursor in red (danger) area −
Also, “MouseEvent” can have the following events −
Let us see an example of MouseEvent onmouseout event −
 Live Demo
<!DOCTYPE html>
<html>
<head>
<title>MouseEvent onmouseout</title>
<style>
   * {
      padding: 2px;
      margin:5px;
   }
   form {
      width:70%;
      margin: 0 auto;
      text-align: center;
   }
   #outer {
      width:70%;
      margin: 0 auto;
      padding: 0;
      text-align: center;
      border:1px solid black;
      height: 105px;
      background-color: #28a745;
   }
   input[type="button"] {
      border-radius: 10px;
   }
   #upper {
      border-bottom: 1px solid black;
      height: 40px;
      margin: 0 0 15px 0;
      background-color: #DC3545;
   }
   #lower {
      border-top: 1px solid black;
      height: 40px;
      margin: 15px 0 0 0;
      background-color: #DC3545;
   }
</style>
</head>
<body>
   <form>
      <fieldset>
         <legend>MouseEvent-onmouseout</legend>
         <div id="outer" onmouseout="gameStart(event)">
         <div id="upper"><h2>Danger</h2></div>
         <div id="lower"><h2>Danger</h2></div>
         </div>
         <div id="divDisplay"></div>
         </fieldset></form>
<script>
   var divDisplay = document.getElementById("divDisplay");
   var textSelect = document.getElementById("textSelect");
   function gameStart(event) {
      var fetchedID = event.relatedTarget.id
         if(fetchedID !== '')
            divDisplay.textContent = 'You are hovering over '+fetchedID+' <div> element';
         }
</script>
</body>
</html>
Hovering over green (safe) area −
Hovering over upper red (danger) area −
Hovering over lower red (danger) area −