读书人

jquery 判断鼠标移动方向解决办法

发布时间: 2012-06-19 14:45:20 作者: rapoo

jquery 判断鼠标移动方向
如何判断鼠标移动方向,了解思想是利用e.clientX记住两次的鼠标坐标,再把二者相减,可以得出鼠标移动方向,问题是弄了很久也不见得效果,希望回复者不要把刚开始的x坐标置为0.懂得请帮忙!不甚感激!

[解决办法]
http://topic.csdn.net/u/20110411/11/02ed0e54-64a7-4fdf-8348-20fb3c3b5247.html
稍微修改下,如果只想计算鼠标移动方向的话,数组中只需要记录一个坐标,onmousemove事件触发时,用当前坐标跟数组中记录的坐标比较一下,计算好方向后更新数组。
[解决办法]

HTML code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>无标题文档</title></head><style type="text/css">html,body { height:100%; }</style><body><script type="text/javascript">var arrPos = new Array();window.onload = function() {    document.getElementsByTagName('body')[0].onmousemove = function(e) {        var x = e.clientX, y = e.clientY;        //判断鼠标运行方向        var  direction = '';        if (arrPos.length > 0) {            if (x > arrPos[0][0]) {                if (y == arrPos[0][1]) direction = '右';                else {                    if (y > arrPos[0][1]) direction = '右下';                    else direction = '右上';                }            }            else {                if (x == arrPos[0][0]) {                    if (y < arrPos[0][1]) direction = '上';                    else {                        if (y > arrPos[0][1]) direction = '下';                    }                }                else {                    if (y == arrPos[0][1]) direction = '左';                    else {                        if (y > arrPos[0][1]) direction = '左上';                        else direction = '左下';                    }                }            }        }                if (arrPos.length < 1) arrPos.push(Array(x,y));        else {            arrPos[0][0] = x;            arrPos[0][1] = y;            document.getElementById('direction').innerHTML = direction;        }    }}</script><div id="direction"></div></body></html> 

读书人网 >JavaScript

热点推荐