Leaflet Mouseout called on MouseOver event

夙愿已清 提交于 2019-12-12 09:34:43

问题


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. 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


回答1:


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'.

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();});
}



回答2:


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:

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

and assigning my-popup className to the popup:

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

I hope this helps someone :)




回答3:


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. 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. 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.

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.



来源:https://stackoverflow.com/questions/20109336/leaflet-mouseout-called-on-mouseover-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!