判断鼠标进入容器的方向

思路:以div容器的中心点作为圆心,以高或宽中小的值作为直径画圆,鼠标进入容器时的点的atan2(y,x)值在这四个象限里分别对应容器边框的下,右,上,左。

function direction(e){
       var w = $(this).width();
       var h = $(this).height();
       var x = (e.pageX - this.offsetLeft - (w / 2)) * (w > h ? (h / w) : 1);
       var y = (e.pageY - this.offsetTop - (h / 2)) * (h > w ? (w / h) : 1);
       var direction = Math.round((((Math.atan2(y, x) * (180 / Math.PI)) + 180) / 90) + 3) % 4; //direction的值为“0,1,2,3”分别对应着“上,右,下,左”
       retrun direction
}

$(“#wrap”).bind(“mouseenter mouseleave”,function(e) {
var direction = direction(e)
});

文章目录
,