【发布时间】:2016-05-24 08:42:21
【问题描述】:
我编写此代码是为了在鼠标悬停时更改标记的图标,并在鼠标悬停时将其改回,但鼠标悬停后似乎永远不会触发 mouseout 事件。
我也提到了这个问题(Leaflet Mouseout called on MouseOver event),但我不知道这是否是这里的问题。如果这是问题,我应该如何解决它。
L.marker([20.123,40,234],{icon:icon}).on('mouseover',function(e){
this.setIcon(highlight);
}).on('mouseout',function(e){
this.setIcon(icon);
}).addTo(map);
编辑 1: 这是完整的代码:
var map = L.map('map').fitWorld();
L.tileLayer("https://api.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token={accessToken}",{
id:'mapankit.137364c3',
accessToken:'pk.eyJ1IjoibWFwYW5raXQiLCJhIjoiY2lramo5anZoMDdjMnVjajdjYWtqbXZ3eiJ9.uR_6t2C2f5Ib9qOPnt_l8Q'}).addTo(map);
var icon = L.divIcon({
html : '<svg width="12px" height="12px"><g><path class="outer" d="M-5 0 A5 5 0 0 1 5 0 A5 5 0 0 1 -5 0" style="stroke-opacity: 1; stroke-width: 2; fill: white; stroke: rgb(0, 198, 228);" transform="translate(6,6)"></path><path class="inner" d="M-2.5 0 A2.5 2.5 0 0 1 2.5 0 A2.5 2.5 0 0 1 -2.5 0" style="stroke-opacity: 1; stroke-width: 0; fill: white; stroke: white;" transform="translate(6,6)"></path></g></svg>',
className : 'foo',
iconAnchor : L.point(6,6)
});
var highlight = L.divIcon({
html : '<svg width="25px" height="25px"><g><path class="outer" d="M-10 0 A10 10 0 0 1 10 0 A10 10 0 0 1 -10 0" style="fill: white; stroke: rgb(0, 198, 228); stroke-width: 2;" transform="translate(12,12)"></path><path class="inner" d="M-5 0 A5 5 0 0 1 5 0 A5 5 0 0 1 -5 0" style="fill: rgb(0, 198, 228); stroke: rgb(0, 198, 228); stroke-opacity: 1;" transform="translate(12,12)"></path></g></svg>',
className : 'bar',
iconAnchor : L.point(12,12)
})
L.marker([20.123,40,234],{icon:icon}).on('mouseover',function(e){
this.setIcon(highlight);
}).on('mouseout',function(e){
this.setIcon(icon);
}).addTo(map);
【问题讨论】:
-
是的,你是对的,它不适用于 DivIcon(但它适用于 Icon)
-
我有动态内容用作图标,因此我使用 divIcon 以便我可以直接提供 HTML
-
有什么办法可以将 html 赋予图标而不使用 divIcon?
-
SVG UI 事件可能无法正确处理。您是否尝试过在
DivIcon中使用非 SVG 内容? -
是的,效果很好。我尝试将 svg 作为文件提供给有效的图标。但在我的用例中,我无法为我创建的每个新图标创建文件。
标签: javascript leaflet