mouse position count

微笑、不失礼 提交于 2019-12-11 19:55:38

问题


Hi I'm using Jquery's mouse position to find the position of the page.

I would like it to have it behave more like latitude and longitude coordinates.

    $().mousemove(function(e){
        $('#detect').html( e.pageX + '° N, '+ e.pageY + '° E' );

    });

I realise this is not true latitude and longitude coordinates. The box would be the webpage, and 0 is absolute middle of the webpage. Moving the mouse into the N/E area would produce this result.

Any help would be appreciated. Thanks.


回答1:


Javascript:

var screenX = $(document).width() / 2;
var screenY = $(document).height() / 2;

$(document).mousemove(function(e){

    var apX = screenX - e.pageX;
    var apY = screenY - e.pageY;

    var latT = (apY>=0) ? 'N' : 'S';
    var lonT = (apX>=0) ? 'W' : 'E';

    apX = Math.round(Math.abs(apX));
    apY = Math.round(Math.abs(apY));

    $('#detect').html( apX  + 'px '+ latT +', '+ apY + 'px '+ lonT  );

});

You can find a test on http://jsfiddle.net/nvwzH/



来源:https://stackoverflow.com/questions/8500111/mouse-position-count

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!