本文介紹了使傳單工具提示可點擊的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我想在 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: '© <a >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" />
<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: '© <a >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" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.3.1/leaflet.js"></script>
<div id='map'></div>
這篇關于使傳單工具提示可點擊的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!