// Define the tooltip container,
// that will be displayed on hover
let tooltipContainer = document.getElementById("tooltip");
// Collect the Child of the tooltip
// container that will store the text
let tooltipTextContainer = tooltipContainer.querySelector(".tooltip-text");
// Collect the tooltip triggers,
// that upon hover will trigger
// tooltip display
let tooltipTriggers = document.querySelectorAll("[data-tooltip]");
// An offset that will be used to
// determine where tp position the tooltip,
// around its trigger element
const tooltipOffset = 300;
// an extra offset for placing the tooltip
// a bit further from trigger
const tooltipHandleSize = 20;
const screenWidth = window.innerWidth;
const screenHeight = window.innerHeight;
// Add mouseover/mouseout events for each element that has a tooltip text
tooltipTriggers.forEach((e) => {
let showToltipFor = e;
showToltipFor.addEventListener("mouseover", () => {
showTooltip(showToltipFor);
});
showToltipFor.addEventListener("mouseout", () => {
hideTooltip();
});
});
function showTooltip(el) {
// get the position on screen
let triggerScreenPosX = el.getBoundingClientRect().left;
let triggerScreenPosY = el.getBoundingClientRect().top;
// get size of element
// will be used to position the tooltips
let triggerWidth = el.offsetWidth;
let triggerHeight = el.offsetHeight;
let tooltipText = el.getAttribute("data-tooltip");
tooltipTextContainer.innerHTML = tooltipText;
tooltipContainer.style.left = triggerScreenPosX + "px";
tooltipContainer.style.top = triggerScreenPosY + "px";
// Check where to place tooltips
// based on trigger's position on screen
// check on X axis
if (triggerScreenPosX < tooltipOffset) {
tooltipContainer.dataset.posX = "left"; // el is closer to left edge
} else if ((screenWidth - triggerScreenPosX) < tooltipOffset) {
// el is closer to right edge
tooltipContainer.dataset.posX = "right";
} else {
// we...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.