且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

向下移动键时,Google会更改点击行为

更新时间:2023-12-02 22:39:28

也许很明显,但是我通过在常规click事件中设置全局变量,然后在DOM click事件中检查设置的变量来解决此问题

Perhaps it's obvious, but I solved this by simply setting a global variable in the normal click event, and then in the DOM click event checking for that variable being set.

这样,如果在适当的情况下可以触发正常的click事件,那么在Shift事件中(如果未按下shift键的情况下)设置变量并在DOM事件中对单击进行操作.

This way, if the circumstances are right for the normal click event to fire, then the variable is set and the click is acted on in the DOM event, if the shift key isn't down.

如果按下Shift键,我们将执行其他操作.

If the shift key is down, we do something else.

无论哪种方式,我们都清除变量.

Either way we clear the variable.

var mapClickFired = false;

map.addListener( "click", function(e) {
    mapClickFired = true; // acted on in DOM listener below
});

// bind to DOM event as well, so we can find out if modifier keys are pressed
google.maps.event.addDomListener( document.getElementById("map"), "click", function(event) {
    // check modifiers
    if ( event.shiftKey ) {
        // do things for shift key down
    }
    else if ( mapClickFired ) {
        // do things for regular map click 
    }
    mapClickFired = false;
});

当然,这确实取决于这两个事件的顺序是否与预期一致.由于没有记录,所以我有点紧张,我创建了一些脆弱的东西.

Of course, this does depend on the ordering of these two events being consistently as expected. Since it's not documented I'm a bit nervous I've created something fragile.