Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

241
Visualizações
SVG path mouseover not working with Leaflet

I'm having an issue where a mouseover event is not working with an SVG path element when using a Leaflet overlay pane. I create 3 SVG elements (using D3.js) - a circle, a rect and a path. They all display fine, and the mouseover events work on the circle and rect, but not the path. If I create the same 3 elements without Leaflet, they all work fine.

Here's the javascript code

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")
  })

Here it is as a JSFiddle

And here's the same code without Leaflet which works as I expect

Can anyone explain why the mouseover event is not working with the path element? Thanks!

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

If you inspect the path in your browser's debugger, you'll see that Leaflet.css contains this...

.leaflet-marker-icon, .leaflet-marker-shadow, .leaflet-image-layer, .leaflet-pane > svg path, .leaflet-tile-container { pointer-events: none; }

So it explicitly turns pointer-events off for all paths that are the direct descendents of SVG tags.

You could override this by adding

.style("pointer-events", "auto")

to the path generation code.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda