简体   繁体   English

在 MouseOver 事件上调用 Leaflet Mouseout

[英]Leaflet Mouseout called on MouseOver event

I have a leaflet map where I'm dynamically adding markers.我有一张传单地图,我在其中动态添加标记。

I want to call the popup for a marker when I hover over it in addition to when I click the marker.除了单击标记时,我还想在将鼠标悬停在标记上时调用该标记的弹出窗口。

My code is:我的代码是:

function makeMarker(){
   var Marker = L.marker...
   Marker.on('mouseover', function(){Marker.bindPopup('HI').openPopup();});

   Marker.on('mouseout', function(){Marker.closePopup();});
}

If I comment out the mouseout line, then the popup appears but then I have to click elswhere to close it.如果我注释掉 mouseout 行,则会出现弹出窗口,但我必须单击 elswhere 将其关闭。 The problem is when I put in the mouseout, at that point, the cursor kinda flickers when it hovers over the marker and nothing shows.问题是当我输入鼠标时,此时光标悬停在标记上时会有点闪烁并且没有任何显示。 I think that the popup is openning but then closing really fast which is why the cursor flickers but I don't know how to fix this我认为弹出窗口正在打开但关闭速度非常快,这就是光标闪烁的原因,但我不知道如何解决这个问题

The popup is actually loading underneath the cursor and 'stealing' the mouse from the Marker, triggering the Marker.mouseout() event, which causes the popup to close and re-triggers the Marker.mouseover() event... and the cycle continues which is why you see the 'flicker'.弹出窗口实际上是在光标下方加载并从标记中“窃取”鼠标,触发 Marker.mouseout() 事件,这会导致弹出窗口关闭并重新触发 Marker.mouseover() 事件......以及循环继续,这就是您看到“闪烁”的原因。

I have seen this happen depending on the zoom level (usually when zoomed right out).我已经看到这种情况取决于缩放级别(通常在缩小时)。

Try adding an offset into your popup options to get it out of the way:尝试在您的弹出选项中添加一个偏移量以摆脱它:

function makeMarker(){
   var Marker = L.marker...
   Marker.on('mouseover', function(){Marker.bindPopup('HI', {'offset': L.point(0,-50)}).openPopup();});

   Marker.on('mouseout', function(){Marker.closePopup();});
}

I know it's an old thread but I've just came across this issue and I thought that I can share my solution.我知道这是一个旧线程,但我刚刚遇到了这个问题,我想我可以分享我的解决方案。 Instead of offsetting the popup, I am preventing the popup from stealing the mouse event using CSS by setting:我没有抵消弹出窗口,而是通过设置来防止弹出窗口使用 CSS 窃取鼠标事件:

.my-popup {pointer-events: none;}

and assigning my-popup className to the popup:并将 my-popup className 分配给弹出窗口:

Marker.on('mouseover', function () {Marker.bindPopup('HI', {className: 'my-popup'}).openPopup();})

I hope this helps someone :)我希望这对某人有所帮助:)

I also recently came across this problem again too.我最近也再次遇到了这个问题。 In case this helps anyone: As of Leaflet 1.0, you can use L.Tooltip instead of popups with mouseover or other mouse-related events.如果这对任何人有帮助:从 Leaflet 1.0 开始,您可以使用 L.Tooltip 而不是带有鼠标悬停或其他鼠标相关事件的弹出窗口。 I've found that it works much more smoothly than making a Popup, and uses less code, especially if you're just opening the popup on hover.我发现它比制作 Popup 更流畅,并且使用的代码更少,特别是如果您只是在悬停时打开弹出窗口。 In your case it might look like:在您的情况下,它可能看起来像:

function makeMarker(){
  var Marker = L.marker...
  Marker.bindTooltip('HI').openPopup();
}

And you can use the permanent boolean flag if you want to keep it open.如果您想让它保持打开状态,您可以使用permanent布尔标志。

In my case I didn't need the popup to open on click (in addition to hover), so that might be a little more complicated.在我的情况下,我不需要在点击时打开弹出窗口(除了悬停),所以这可能会更复杂一些。 However L.Tooltip has become very widely used and there are other SO questions that deal with this issue.然而,L.Tooltip 已变得非常广泛使用,还有其他 SO 问题可以解决这个问题。

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

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