[추억1314] 마우스 오프 방향 체크

12931 단어 마우스

그냥 효과 보고 여기.

<html>

<head>        </head>

<body>

<div id="msg">      ,    。</div>

<div id="wrap" style="width:200px; height: 100px; line-height:30px; margin: 100px auto; text-align: center; border: 1px solid #99bbe8;">↖↑↗<br/>← →<br/>↙↓↘</div>

<script src="http://common.cnblogs.com/script/jquery.js"></script>

<script>

$("#wrap").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 direction = Math.round((((Math.atan2(y, x) * (180 / Math.PI)) + 180) / 45) + 3) % 8;

    var dirName = ['  ', ' ', '  ', ' ', '  ', ' ', '  ', ' '];

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

        $("#msg").html(dirName[direction]+'-  ');

    }else{

        $('#msg').html(dirName[direction]+'-  ');

    }

});

</script>

</body>

</html>

좋은 웹페이지 즐겨찾기