简体   繁体   English

如何使用 leaflet 1.7.1 禁用弹出提示上的事件

[英]How to disable events on the popup tip with leaflet 1.7.1

I display a marker on a map with a popup window in which an image is displayed with a hyperlink.我在 map 上显示一个标记,并弹出一个 window ,其中显示带有超链接的图像。
When the mouse in on the tip, an hand is displayed (why?) and if I click then the popup is closed??当鼠标放在尖端时,会显示一只手(为什么?),如果我单击,则弹出窗口关闭??

I added in my css the disable of the events on the tip but that seems to not be the solution.我在我的 css 中添加了禁用提示上的事件,但这似乎不是解决方案。

var options = {
   'className': 'custom-popup'
}
popup = "<a href='" + (BASE_URL || '') + m.link + "'>" + "<img src='" +  (BASE_URL || '') + m.image + "' /></a>"; 
marker.bindPopup(popup,options).openPopup();

The css custom-popup supersedes the leaflet.css: css 自定义弹出窗口取代了 leaflet.css:

.custom-popup div.leaflet-popup-content {
  min-Width: 300px;
  max-Width: 300px;
  max-Height: 500px;
}
.custom-popup .leaflet-popup-content-wrapper {
  background:#2c3e50;
  color:#fff;
  font-size:16px;
  line-height:24px;
}
.custom-popup .leaflet-popup-tip-container {
  width:40px;
  height:20px;
  pointer-events: none;
}
.custom-popup .leaflet-popup-tip {
  background:#2c3e50;
}   

When the mouse in on the tip, an hand is displayed (why?) and if I click then the popup is closed??当鼠标放在尖端时,会显示一只手(为什么?),如果我单击,则弹出窗口关闭??

Precisely because pointer-events: none is the default for .leaflet-popup-tip-container .正是因为pointer-events: none.leaflet-popup-tip-container的默认值。 The tip does not trigger events, so when a user clicks on the tip, the click goes straight through to the underlying element (the Leaflet map), which does trigger events.提示不会触发事件,因此当用户点击提示时,点击会直接到达底层元素(Leaflet 映射),它会触发事件。 Then, the default behaviour when clicking on the map itself is to close the active popup.然后,单击 map 本身时的默认行为是关闭活动弹出窗口。

Also related: There is no such thing as "disabling" events and parts of a Leaflet popup .也相关: 没有“禁用”事件Leaflet 弹出窗口的部分

The approach here would be to apply the CSS rule pointer-events: auto or pointer-events: initial to .leaflet-popup-tip .这里的方法是将 CSS 规则pointer-events: autopointer-events: initial应用于.leaflet-popup-tip Pointer interactions on the tip shall trigger DOM events that are not listened to and therefore do nothing .提示上的指针交互触发不被监听的 DOM 事件,因此什么也不做

Don't add the popup directly to marker, create a popup "layer" and open it, when clicked on the marker:不要将弹出窗口直接添加到标记,创建一个弹出“图层”并在单击标记时打开它:

// a global variable
var popup = L.popup({'className': 'custom-popup', closeOnClick: false, autoClose: false, closeButton: true});

function openPopup(e){
    var marker = e.target;
    popup.options.offset = marker.options.icon.options.popupAnchor;
    popup.setContent(marker._popupContentTemp).setLatLng(marker.getLatLng()).addTo(map)
}


//your marker creation with click event
marker = new L.marker(latlng).addTo(lmap);
marker.on('click',openPopup, this)
marker._popupContentTemp = "<a href='" + (BASE_URL || '') + m.link + "'>" + "<img src='" +  (BASE_URL || '') + m.image + "' /></a>"; 

Now the popup is opened with the custom content and can't closed by clicking on the marker现在弹出窗口使用自定义内容打开,无法通过单击标记关闭

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

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