【问题标题】:Mouseover and Mouseout listener for Google Maps markerGoogle Maps 标记的 Mouseover 和 Mouseout 侦听器
【发布时间】:2023-03-10 04:15:01
【问题描述】:

我想在鼠标悬停在标记上时具有弹跳效果,并在鼠标离开时停止动画。

我正在尝试对这样的侦听器使用 mouseover 和 mouseout 事件:

google.maps.event.addListener(marker, 'mouseover', function() {
  this.setAnimation(google.maps.Animation.BOUNCE);
});

google.maps.event.addListener(marker, 'mouseout', function() {
  this.setAnimation(null);
});

但这看起来很奇怪。 我无法用语言解释错误的行为 - 请看我录制的这个 15 秒视频:

===> http://youtu.be/Hcy8823nNQU

我需要的可能是 mouseleave 而不是 mouseout,但他们的 API 没有提供该事件。

【问题讨论】:

    标签: google-maps google-maps-api-3 google-maps-markers


    【解决方案1】:

    关键是动画没有设置的时候才设置,如:

    google.maps.event.addListener(marker, 'mouseover', function() {
        if (this.getAnimation() == null || typeof this.getAnimation() === 'undefined') {
    
            /* 
            Because of the google maps bug of moving cursor several times over and out of marker
            causes bounce animation to break - we use small timer before triggering the bounce animation
            */
    
            clearTimeout(bounceTimer);
    
            var that = this;
    
            bounceTimer = setTimeout(function(){
                 that.setAnimation(google.maps.Animation.BOUNCE);
            },
            500);
    
        }
    });
    
    google.maps.event.addListener(marker, 'mouseout', function() {
    
         if (this.getAnimation() != null) {
            this.setAnimation(null);
         }
    
         // If we already left marker, no need to bounce when timer is ready
         clearTimeout(bounceTimer);
    
    });
    

    我为你创建了一个JS fiddle

    已编辑:

    似乎使用标记作为 draggable: false 会破坏功能,所以如果您希望动画仍然有效,您还需要添加 optimized: false ,更新我的小提琴以包含这些。我还看到如果标记动画切换进出太快会出现错误,在我们开始弹跳动画之前添加了小计时器来指示,似乎解决了这个问题。

    【讨论】:

    • 感谢您的快速回复。对我来说它不起作用,肯定我在其他代码的某个地方有问题。例如,当我从您的 JS 小提琴中删除条件时,它仍在工作。
    • 哦,我明白了。我需要添加“draggable:true”,然后它就可以工作了!看,这是你的小提琴,但我将 draggable 设置为 false,我们看到了我遇到的问题 - jsfiddle.net/Mas4D/1 但为什么呢?我不希望标记可拖动,嗯。
    • 我马上回答,我似乎发现了谷歌地图的一些错误以及如何克服它们。 :)
    猜你喜欢
    • 1970-01-01
    • 2013-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-29
    • 1970-01-01
    • 2021-04-05
    • 2013-07-27
    相关资源
    最近更新 更多