mouseenter and mouseover and mouseout and mouseleave difference

1、mouseover与mouseenter

In common :
When both are no children, the behavior of both is the same, but both the internal child elements, the behavior is different.

Different points:
mouseover event: whether the mouse pointer enters the selected element or its child elements, will trigger its parent mouseover event. mouseenter event: only when the mouse pointer enters the selected elements to trigger mouseenter event.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    #box1 {
      width:200px;
      height: 200px;
      background-color: aquamarine;
    }
    #box2 {
      width:200px;
      height: 200px;
      background-color: brown;
    }
    #ch1 {
      width:150px;
      height: 100px;
      background-color:cornsilk;
      margin: 0 auto;
    }
    #ch2 {
      width:150px;
      height: 100px;
      background-color:darkgrey;
      margin: 0 auto;
    }
  </style>
</head>
<body>
    <div id="box1">
      <div id="ch1"></div>
    </div>
    <div id="box2">
      <div id="ch2"></div>
    </div>
    <script>
      var box1 = document.getElementById('box1');
      var box2 = document.getElementById('box2');
      var ch1 = document.getElementById('ch1');
      var ch2 = document.getElementById('ch2');
      var a = 1;
      var b = 1;
      var c = 1;
      var d = 1;

      function fnb1(){
        ch1.innerText = '父节点mouseover次数' + ++a + "\n" +'子节点mouseover次数'+ c;
      }

      function fnb2(){
        ch2.innerText = '父节点mouseenter次数' + ++b + "\n" +'子节点mouseenter次数'+ d;
      }

      function fnc1(){
        ch1.innerText = '父节点mouseover次数' + a + "\n" +'子节点mouseover次数'+ ++c;
      }

      function fnc2(){
        ch2.innerText = '父节点mouseenter次数' + b + "\n" +'子节点mouseenter次数'+ ++d;
      }

      box1.addEventListener('mouseover', fnb1,false)
      box2.addEventListener('mouseenter', fnb2,false)

      ch1.addEventListener('mouseover', fnc1,false)
      ch2.addEventListener('mouseenter', fnc2,false)
    </script>
</body>
</html>

Page is shown below:
Page display

2、mouseout和mouseleave

Common:
When both are no children, the behavior of both is the same, but both the internal child elements, the behavior is different.
Different points:
mouseout events: whether the mouse pointer leaves the selected element or any child elements, will trigger mouseout event.
mouseleave event: Only the selected elements in the mouse pointer leaves, will trigger mouseleave event.

And the effect here is not the same as the above demo code.

Published 58 original articles · won praise 20 · views 110 000 +

Guess you like

Origin blog.csdn.net/fly_wugui/article/details/104842742