【发布时间】:2021-12-01 13:16:59
【问题描述】:
我遇到了一个问题,即当使用 Leaflet 覆盖窗格时,鼠标悬停事件不适用于 SVG path 元素。我创建了 3 个 SVG 元素(使用 D3.js)——circle、rect 和 path。它们都显示正常,并且鼠标悬停事件适用于circle 和rect,但不适用于path。如果我在没有 Leaflet 的情况下创建相同的 3 个元素,它们都可以正常工作。
这是javascript代码
var mymap = L.map('mapid').setView([51.505, -0.09], 13);
L.tileLayer("https://tile.openstreetmap.org/{z}/{x}/{y}.png").addTo(mymap);
//initialize svg to add to map
L.svg({clickable:true}).addTo(mymap)// we have to make the svg layer clickable
const overlay = d3.select(mymap.getPanes().overlayPane)
const svg = overlay.select('svg').attr("pointer-events", "auto")
svg.append('path')
.attr('d', 'M 110 110 H 190 V 190 H 110 L 110 110')
.style("fill", "green")
.attr("stroke", "black")
.on('mouseover', () => {
console.log("mouseover path");
d3.select(event.currentTarget).style("fill", "blue")
})
.on('mouseout', () => {
d3.select(event.currentTarget).style("fill", "green")
})
svg.append('circle')
.attr("cx", 300)
.attr("cy", 200)
.attr("r", 75)
.style("fill", "yellow")
.attr("stroke", "black")
.on('mouseover', () => {
console.log("mouseover circle");
d3.select(event.currentTarget).style("fill", "blue")
})
.on('mouseout', () => {
d3.select(event.currentTarget).style("fill", "yellow")
})
svg.append('rect')
.attr("x", 500)
.attr("y", 200)
.attr("width", 75)
.attr("height", 75)
.style("fill", "red")
.attr("stroke", "black")
.on('mouseover', () => {
console.log("mouseover rect");
d3.select(event.currentTarget).style("fill", "blue")
})
.on('mouseout', () => {
d3.select(event.currentTarget).style("fill", "red")
})
这里是JSFiddle
这是没有 Leaflet 的相同代码 works as I expect
谁能解释为什么 mouseover 事件不适用于path 元素?谢谢!
【问题讨论】:
标签: javascript svg leaflet