SoFunction
Updated on 2025-03-01

How to judge the direction of mouse movement in and out

The effect of moving in and out of the mouse on the PC side is very good. Here is a way to judge the moving in and out of the mouse. I will post an article with effects when I have time. However, it is better to take it as a fish than to teach it. With this method, isn’t the effect still so easy?

Code:

<!DOCTYPE html>
<html lang="zh">
 <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></title>
 <style type="text/css">
 * {
 padding: 0;
 margin: 0;
 }
 
 .ul-box {
 width: 345px;
 margin: 20px auto;
 background-color: #f5f5f5;
 overflow: auto;
 }
 
 .li-item {
 width: 100%;
 height: 134px;
 margin-bottom: 10px;
 border-radius: 4px;
 }
 
 .li-item1 {
 background: linear-gradient(90deg, #7f90e3 0%, #8b9bff 100%);
 }
 </style>
 </head>
 <body>
 <ul  class="ul-box">
 <li class="li-item li-item1" onmouseleave="derEvent(event, 'leave')" onmouseenter="derEvent(event, 'enter')"></li>
 </ul>
 </body>
 <script type="text/javascript">
 /**
  * @param {Object} event Current event source information
  * @return {Number} d Direction top 0 right 1 bottom 2 left 3
  */
 function getDer(event) {
 let ele = 
 var d;
 var w = ,
 h = ,
 l = ,
 t = ,
 x = ( - l - w / 2) * (w > h ? (h / w) : 1),
 y = ( - t - h / 2) * (h > w ? (w / h) : 1);
 d = (((Math.atan2(y, x) * (180 / ) + 180) / 90) + 3) % 4;
 return d;
 // top 0 right 1 bottom 2 left 3
 }
 /**
  * @param {Object} event event source information
  * @param {Object} type type leave leave color enter
  */
 function derEvent(event, type) {
 let d = getDer(event)
 let txt = ''
 switch (d) {
 case 0:
  txt = 'superior'
  break;
 case 1:
  txt = 'right'
  break;
 case 2:
  txt = 'Down'
  break;
 case 3:
  txt = 'Left'
  break;
 default:
  break;
 }
 if (type === 'leave') {
 (txt, 'leave')
 } else {
 (txt, 'Enter')
 }
 }
 </script>
</html>

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.