Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

118
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!