使传单工具提示可点击

Making Leaflet tooltip clickable(使传单工具提示可点击)
本文介绍了使传单工具提示可点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我想在 Leaflet 地图(不带标记)上添加 工具提示 并使其可点击.以下代码添加了一个工具提示,但它不可点击:

I want to add tooltips on a Leaflet map (without markers) and make them clickable. The following code adds a tooltip but it's not clickable:

var tooltip = L.tooltip({
        direction: 'center',
        permanent: true,
        interactive: true,
        noWrap: true,
        opacity: 0.9
    });
tooltip.setContent( "Example" );
tooltip.setLatLng(new L.LatLng(someLat, someLon));
tooltip.addTo(myLayer);
tooltip.on('click', function(event) {
    console.log("Click!");
});

我怎样才能让它工作?

推荐答案

要接收对 L.Tooltip 对象的点击,您需要:

To receive clicks on a L.Tooltip object, you'll need to :

  • 在关联的 DOM 对象上设置监听器:

  • set up a listener on the associated DOM object :

var el = tooltip.getElement();
el.addEventListener('click', function() {
   console.log("click");
});

  • 删除 pointer-events: none 在该元素上设置的属性:

  • remove the pointer-events: none property set on that element:

    var el = tooltip.getElement();
    el.style.pointerEvents = 'auto';
    

  • 到目前为止的演示

    var map = L.map(document.getElementById('map')).setView([48.8583736, 2.2922926], 4);
    L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
            attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
    }).addTo(map);
    
    var tooltip = L.tooltip({
        direction: 'center',
        permanent: true,
        interactive: true,
        noWrap: true,
        opacity: 0.9
    });
    tooltip.setContent( "Example" );
    tooltip.setLatLng(new L.LatLng(48.8583736, 2.2922926));
    tooltip.addTo(map);
    
    var el = tooltip.getElement();
    el.addEventListener('click', function() {
        console.log("click");
    });
    el.style.pointerEvents = 'auto';

    html, body {
      height: 100%;
      margin: 0;
    }
    #map {
      width: 100%;
      height: 180px;
    }

    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.3.1/leaflet.css"/>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.3.1/leaflet.js"></script>
       
    <div id='map'></div>

    如果您想创建组件或直接监听工具提示对象,您可以扩展 L.Tooltip 以将这些更改直接烘焙到定义中:

    If you want to create a component or listen directly to a tooltip object, you can extend L.Tooltip to bake those alterations directly into the definition:

    L.ClickableTooltip = L.Tooltip.extend({
        onAdd: function (map) {
            L.Tooltip.prototype.onAdd.call(this, map);
    
            var el = this.getElement(),
                self = this;
    
            el.addEventListener('click', function() {
                self.fire("click");
            });
            el.style.pointerEvents = 'auto';
        }
    });
    
    var tooltip = new L.ClickableTooltip({
        direction: 'center',
        permanent: true,
        noWrap: true,
        opacity: 0.9
    });
    tooltip.setContent( "Example" );
    tooltip.setLatLng(new L.LatLng(48.8583736, 2.2922926));
    tooltip.addTo(map);
    
    tooltip.on('click', function(e) {
        console.log("clicked", JSON.stringify(e.target.getLatLng()));
    });
    

    还有一个演示

    L.ClickableTooltip = L.Tooltip.extend({
    
        onAdd: function (map) {
            L.Tooltip.prototype.onAdd.call(this, map);
    
            var el = this.getElement(),
                self = this;
    
            el.addEventListener('click', function() {
                self.fire("click");
            });
            el.style.pointerEvents = 'auto';
        }
    });
    
    
    var map = L.map(document.getElementById('map')).setView([48.8583736, 2.2922926], 4);
    L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
            attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
    }).addTo(map);
    
    var tooltip = new L.ClickableTooltip({
        direction: 'center',
        permanent: true,
        noWrap: true,
        opacity: 0.9
    });
    tooltip.setContent( "Example" );
    tooltip.setLatLng(new L.LatLng(48.8583736, 2.2922926));
    tooltip.addTo(map);
    
    tooltip.on('click', function(e) {
        console.log("clicked", JSON.stringify(e.target.getLatLng()));
    });

    html, body {
      height: 100%;
      margin: 0;
    }
    #map {
      width: 100%;
      height: 180px;
    }

    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.3.1/leaflet.css"/>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.3.1/leaflet.js"></script>
       
    <div id='map'></div>

    这篇关于使传单工具提示可点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

    本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

    相关文档推荐

    How do I can get a text of all the cells of the table using testcafe(如何使用 testcafe 获取表格中所有单元格的文本)
    node_modules is not recognized as an internal or external command(node_modules 未被识别为内部或外部命令)
    How can I create conditional test cases using Protractor?(如何使用 Protractor 创建条件测试用例?)
    PhantomJS and clicking a form button(PhantomJS 并单击表单按钮)
    Clicking #39;OK#39; on alert or confirm dialog through jquery/javascript?(在警报上单击“确定或通过 jquery/javascript 确认对话框?)
    QunitJS-Tests don#39;t start: PhantomJS timed out, possibly due to a missing QUnit start() call(QunitJS-Tests 不启动:PhantomJS 超时,可能是由于缺少 QUnit start() 调用)