简体   繁体   English

悬停Google Map时禁用鼠标EventListener

[英]Disable mouse EventListener when hovering a Google Map

I added 2 EventListener(s) in order to control the scroll and have it fluid and smooth on Chrome (especially), which by default has a horrible behavior. 我添加了2个EventListener,以控制滚动并使其在Chrome上流畅(尤其是平滑)(默认情况下),这在默认情况下具有可怕的行为。

if (window.addEventListener) window.addEventListener('DOMMouseScroll', wheel, false);
window.onmousewheel = document.onmousewheel = wheel;

var time = 1300;
var distance = 270;

function wheel(event) {
    if (event.wheelDelta) delta = event.wheelDelta / 120;
    else if (event.detail) delta = -event.detail / 3;

    handle();
    if (event.preventDefault) event.preventDefault();
    event.returnValue = false;
}

function handle() {

    $('html, body').stop().animate({
        scrollTop: $(window).scrollTop() - (distance * delta)
    }, time);
}


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

    switch (e.which) {
        //up
        case 38:
            $('html, body').stop().animate({
                scrollTop: $(window).scrollTop() - distance
            }, time);
            break;

            //down
        case 40:
            $('html, body').stop().animate({
                scrollTop: $(window).scrollTop() + distance
            }, time);
            break;
    }
});

Everything's working perfectly except that I need to add a Google Maps API v3 map. 一切工作正常,除了我需要添加Google Maps API v3地图。 And it has another EventListener that zooms in and out when mouse is hovering the map and you use the mouse wheel. 它还有另一个EventListener,当鼠标悬停在地图上并且您使用鼠标滚轮时,它可以放大和缩小。 How can I disable my script when mouse is hovering the map? 当鼠标悬停在地图上时,如何禁用脚本? Here's the link the my page (you can see the behavior), live: http://www.rendezvousroma.it/new/contatti.php 这是我的页面(您可以看到其行为)的实时链接: http : //www.rendezvousroma.it/new/contatti.php

(I'm not sure if I understand the question correctly) (我不确定我是否正确理解了这个问题)

For me your script doesn't work at all when the mouse is over the map. 对我来说,当鼠标悬停在地图上时,您的脚本根本不起作用。 When I use the mousewheel over the map the map will be zoomed but the page doesn't scroll(I guess that's the desired behaviour) 当我在地图上使用鼠标滚轮时,地图将被缩放,但页面不会滚动(我想这是所需的行为)

There is only the queue of the currently running animation that will scroll the page when you are over the map. 当您在地图上时,只有当前正在运行的动画队列会滚动页面。 When this is what you want to avoid, observe the mouseover -event of the map and stop the animation: 如果要避免这种情况,请观察地图的mouseover -event事件并停止动画:

  google.maps.event.addListener(map,'mouseover',function(e){
    $('html, body').stop();
  });

To prevent the page from scrolling in Firefox add this too: 为了防止页面在Firefox中滚动,请添加以下内容:

  google.maps.event.addDomListener(map.getDiv(),'DOMMouseScroll',function(e){
    e.stopPropagation();
  });

It should prevent the event from bubbling up to the document. 它应防止事件冒泡到文档中。

将其放在div: style="user-select: none; pointer-events: none;"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM