简体   繁体   English

仅当未显示弹出窗口时才在悬停时显示传单工具提示

[英]Show leaflet tooltip on hover only when popup is not being shown

I have a tooltip with a short text only description and a popup with a longer formatted description bound to a marker on a leaflet map.我有一个带有简短文本描述的工具提示和一个带有较长格式描述的弹出窗口,该描述绑定到传单地图上的标记。

The tooltip shows on hover and the popup shows when you click on the place marker.悬停时会显示工具提示,单击位置标记时会显示弹出窗口。 When the larger popup is visible there is no need to show the tooltip.当较大的弹出窗口可见时,无需显示工具提示。 Can I disable the tooltip when the popup is visible and how do I do this?当弹出窗口可见时,我可以禁用工具提示吗?我该怎么做?

Here is the code I have so far:这是我到目前为止的代码:

var marker = L.marker(location);
marker.bindPopup("Long description with extra formatting ...");
marker.bindTooltip("Short description");

You can add custom handlers for the tooltip and popup.您可以为工具提示和弹出窗口添加自定义处理程序。 With the leaflet method isPopupOpen() which return true or false you can descide if you open the tooltip or not.使用返回 true 或 false 的传单方法isPopupOpen()您可以决定是否打开工具提示。

function customTip() {
    this.unbindTooltip();
    if(!this.isPopupOpen()) this.bindTooltip('Short description').openTooltip();
}

function customPop() {
    this.unbindTooltip();
}

var marker = L.marker(location);
marker.bindPopup('Long description with extra formatting ...');
marker.on('mouseover', customTip);
marker.on('click', customPop);

I use another solution in my project.我在我的项目中使用了另一种解决方案。 I set the tooltip opacity acording to this.isPopupOpen() .我根据this.isPopupOpen()设置了工具提示不透明度。 For me this works good, because I don't want to always set the tooltip content again.对我来说这很好用,因为我不想总是再次设置工具提示内容。 To hide the tooltip instantly on the click event, set the opacity to 0 on click.要在单击事件上立即隐藏工具提示,请在单击时将不透明度设置为 0。

function showHideTooltip()
{
        var mytooltip = this.getTooltip();
        if(this.isPopupOpen())
        {      
            // Popup is open, set opacity to 0 (invisible)
            mytooltip.setOpacity(0.0);
        }
        else
        {
            // Popup is cosed, set opacity back to visible
            mytooltip.setOpacity(0.9);
        }
}

function clickHideTooltip()
{
        var mytooltip = this.getTooltip();
        mytooltip.setOpacity(0.0);
}

var marker = L.marker(location);
marker.bindPopup("Long description with extra formatting ...");
marker.bindTooltip("Short description");
marker.on('mouseover', showHideTooltip);
marker.on('click', clickHideTooltip);

I used the popupopen and popupclose events to manipulate the tooltip visibility.我使用popupopenpopupclose事件来操纵工具提示的可见性。 This is a good generic solution that doesn't involve extending the standard classes and still respects all the standard configuration and options around popups and tooltips.这是一个很好的通用解决方案,它不涉及扩展标准类,并且仍然尊重弹出窗口和工具提示周围的所有标准配置和选项。

map.on('popupclose', function (e) {

    // make the tooltip for this feature visible again
    // but check first, not all features will have tooltips!
    var tooltip = e.popup._source.getTooltip();
    if (tooltip) tooltip.setOpacity(0.9);

});

map.on('popupopen', function (e) {

    var tooltip = e.popup._source.getTooltip();
    // not all features will have tooltips!
    if (tooltip) 
    {
        // close the open tooltip, if you have configured animations on the tooltip this looks snazzy
        e.target.closeTooltip();
        // use opacity to make the tooltip for this feature invisible while the popup is active.
        e.popup._source.getTooltip().setOpacity(0);
    }

});

NOTE: Took a bit of effort to track down the actual events this solution to a different issue pointed me in the right direction: https://stackoverflow.com/a/16707921/1690217注意:花了一些努力来追踪实际事件,这个解决方案为不同的问题指明了正确的方向: https : //stackoverflow.com/a/16707921/1690217

In my case I have bound the tooltip and the popup to have the same content, so I want to hide the tooltip to suppress the redundant information.就我而言,我已将工具提示和弹出窗口绑定为具有相同的内容,因此我想隐藏工具提示以抑制冗余信息。 In the following greenshot you can see the popup for one shape and the tooltip on hover over the other shapes, it looks messy when that tooltip tries to show under the existing popup when you hover over the feature that triggered the popup.在下面的绿图中,您可以看到一个形状的弹出窗口和悬停在其他形状上的工具提示,当您将鼠标悬停在触发弹出窗口的功能上时,该工具提示试图显示在现有弹出窗口下时,它看起来很混乱。

另一个形状的弹出窗口和工具提示

You can use my solution to this problem:您可以使用我的解决方案来解决此问题:

marker.on('click', function () {
    this.getTooltip().setOpacity(this.isPopupOpen() ? 0 : .9);
});

marker.getPopup().on('remove', function () {
    this._source.getTooltip().setOpacity(0.9);
});

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

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