判断鼠标进入容易的方向,js判断划入方向

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>判断鼠标进入容易的方向,js判断划入方向</title>
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script>
$(function() {
	$("#box").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);
		var y=(e.pageY-this.offsetTop-(h/2))*(h>w?(w/h):1);
		var dirNum=Math.round((((Math.atan2(y,x)*(180/Math.PI))+180)/90)+3)%4;
		/*
			dirNum:0,1,2,3 ---> t r b l
			以上代码是网友分享
			以下代码是模仿拉勾网首页的一个小效果做的
		*/
		var eventType = e.type;
		var aPos=[{left:0,top:-200},{left:200,top:0},{left:0,top:200},{left:-200,top:0}];
		if(eventType == 'mouseenter'){
			$("#box2").css(aPos[dirNum]).stop(true,true).animate({left:0,top:0},200);
		}else{
			$("#box2").stop(true,true).animate(aPos[dirNum],200);
		}
	});
});

</script>
<style>
#box{width: 200px;height: 200px;border:1px solid #999;margin: 200px auto;position:relative;zoom:1;overflow: hidden;}
#box1{height: 200px;background-color: orange;text-align: center;line-height: 200px;}
#box2{position: absolute;left: -200px;top: -200px;width: 200px;height: 200px;background-color: red;text-align: center;line-height: 200px;}
</style>
</head>
<body>


<div id="box">
	<div id="box1">
		#box1
	</div>
	<div id="box2">
		#box2
	</div>
</div>

</body>
</html>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值