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

解决方案 »

  1.   

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

  2.   

    如果外层容器有relative 或 absolute 会有些影响的
      

  3.   

    的确,有用绝对定位,因为想做成像iphone那样的滑动效果!
      

  4.   

    <!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>