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

105
Visualizações
Trying to create a styled tooltip for an inline SVG

New to Js I am building an interactive pronunciation guide through an inline svg.

You can view it here: https://codepen.io/r-pg/pen/OJgoXWQ

I am currently trying to add basic styled tooltips which will display an associated sentence. I have bound its coordinates relative to the cursor position.

The js I am using for this is here:

function tooldef() {
  let t1 = document.querySelector('text[data-tooltip]');
  let t2 = document.querySelector('text:nth-of-type(2)');

  t1.addEventListener('onmousemove', showToolTip(evt));
  t2.addEventListener('onmousemove', showToolTip(evt));
}

function showToolTip(evt) {
  let t = evt.currentTarget;
  let phrase = t.getAttribute('data-tooltip');

  document.getElementById('tooltip').innerHTML = phrase;
  tooltip.style.display = "block";

  tooltip.style.left = evt.offsetX + 0 + 'px';
  tooltip.style.top = evt.offsetY + 0 + 'px';
  /* console.log(tooltip);*/
  console.log(evt.offsetX);
  console.log(evt.offsetY);
}

function hideToolTip(evt) {
  tooltip.style.display = "none";
}

This functions perfectly fine when loaded in through codepen or locally however when I apply this to Wordpress it pushes the tooltip far to the right ad I cannot Identify why. Is there an issue with the code or is this an internal problem within WP?

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

0

Your code for positioning the tooltip assumes a couple of things:

  1. That your SVG is at 100% scale.
  2. That your SVG is at the top left of the page

If you are embedding the SVG inside other content, then one or both of those will not be true any more.

You are attaching the mousemove events to SVG <text> elements. So the coordinates returned in the event will be in SVG coordinate space. If the SVG is scaled to any size other than 1070 x 900, then your event coordinates won't match up with page coordinates.

If you are always displaying the SVG at 1070 x 900, then your fix might be as simple as just getting the page offset of the SVG, then add that X and Y position to the position returned in the event.

However if you are scaling the SVG, then you will need to convert the coordinates from SVG coordinates to page coordinates. There are many other questions on here about that topic. For example, this one: https://stackoverflow.com/a/48354404/1292848

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