如何从谷歌地图事件访问光标位置(即页面的x和y轴)

how to access cursor position (ie; x and y axis of page) from google map event

本文关键字:谷歌地图 事件 访问 位置 光标      更新时间:2023-09-26

我想在我的谷歌地图事件中访问光标(页面)的x和y轴。var小鼠;

代码:

var mouse;
function addEvent(message) {
    google.maps.event.addListener(region1, 'mouseover', function (event) {
        var x = mouse.x + 20 + 'px';
        var y = mouse.y + 20 + 'px';
        var div = $('<div id="div1">').css({
            "position": "absolute",
            "left": x,
            "top": y
        });
        div.append(message);
        $(document.body).append(div);
    });
}
document.addEventListener('mousemove', function (e) {
    var _x = e.clientX || e.pageX;
    var _y = e.clientY || e.pageY;
    mouse = { x: _x, y: _y };
}, false)

如何获得与页面左上角相关的X和Y坐标:

使用mousemove事件

window.onload = init;
function init() {
    if (window.Event) {
    document.captureEvents(Event.MOUSEMOVE);
    }
    document.onmousemove = getCursorXY;
}
function getCursorXY(e) {
    document.getElementById('cursorX').value = (window.Event) ? e.pageX : event.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
    document.getElementById('cursorY').value = (window.Event) ? e.pageY : event.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
}

然后只需设置两个元素cursorXcursorY即可显示坐标。