判断鼠标进入的方向

 $("div").on("mouseenter mouseleave",function(e) {

                     var w = $(this).width(); // 得到盒子宽度

                     var h = $(this).height();// 得到盒子高度

                     var x = (e.pageX - this.offsetLeft - (w / 2)) * (w > h ? (h / w) : 1);

                     // 获取x值

                     var y = (e.pageY - this.offsetTop - (h / 2)) * (h > w ? (w / h) : 1);

                     // 获取y值

                     var direction = Math.round((((Math.atan2(y, x) * (180 / Math.PI)) + 180) / 90) + 3) % 4; //direction的值为“0,1,2,3”分别对应着“上,右,下,左”

                     // 将点的坐标对应的弧度值换算成角度度数值

                     var dirName = new Array('上方','右侧','下方','左侧');

                     if(e.type == 'mouseenter'){

                         $(this).html(dirName[direction]+'进入');

                     }else{

                         $(this).html(dirName[direction]+'离开');

                     }

});

 

posted @ 2016-11-23 13:42  Bryan-S  阅读(211)  评论(0)    收藏  举报