Skip to content

Instantly share code, notes, and snippets.

@xingmolu
Created April 17, 2014 11:34
Show Gist options
  • Save xingmolu/10976085 to your computer and use it in GitHub Desktop.
Save xingmolu/10976085 to your computer and use it in GitHub Desktop.
判断鼠标从什么方向进入容器
$("#wrap").bind("mouseenter,mouseleave", function() {
var w = $(this).width();
var he = $(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" 分别对应着"上右下左"
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment