From 426cb2e8ba140b11ca57bc6adb618dd1ec5b9532 Mon Sep 17 00:00:00 2001 From: ache Date: Sun, 21 Jul 2024 04:21:08 +0200 Subject: Add tooltip on hover ! 👀 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.svelte | 46 +++++++++++++++++++++++++++++++++++++++++----- src/app.css | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 5 deletions(-) diff --git a/src/App.svelte b/src/App.svelte index dc81785..2086708 100644 --- a/src/App.svelte +++ b/src/App.svelte @@ -15,7 +15,7 @@ }) ); - let france = France + let france = France; let currentDepGuessed = -1; let listGuesses = [] let codeSelected; @@ -30,6 +30,9 @@ let canImprove = true; let feedbackString = ""; + let fr_svg; // France SVG binding + let info; // tooltip div binding + function addClassGuess(dep, className) { if(["75", "92", "93", "94"].includes(dep)) { addClassGuess(dep + "-bis", className) @@ -55,6 +58,7 @@ winCount = 0; guessCount = 0; + // Clean the map of any previous style for(let dep of listDep) { removeClassGuess(dep, "win") removeClassGuess(dep, "miss") @@ -113,7 +117,6 @@ autocomplete.value = ""; } - function replaceSlide({ duration = 200, pixels = 200} = {}) { function send(node) { return function() { @@ -149,6 +152,38 @@ function showConfetti() { jsConfetti.addConfetti() } + + function initTooltip() { + /** + * Set the tooltip event listener to show the department name on hover. + */ + const deps = fr_svg.getElementsByTagName("path"); + + for(let dep of deps) { + if(dep.id.startsWith("dep_")) { + const code = dep.id.slice(4).replace("-bis", ""); + + dep.addEventListener("mouseenter", () => { + info.classList.add("active") + info.innerHTML = `${departements[code]['name']}` + }); + + dep.addEventListener("mouseleave", () => { + info.classList.remove("active") + }); + dep.addEventListener("mousemove", e => { + // info.style.transform = "translate3d(" + e.layerX + "px," + e.layerY + "px, 0)"; + info.style.top = `${e.pageY - 70}px`; + info.style.left = `${e.pageX - 0}px`; + }); + } + } + } + + onMount(() => { + initTooltip(); + }); +
@@ -160,7 +195,7 @@

Devine quel est ce département : 

{/if} - + {#if currentDepGuessed == -1}
@@ -174,7 +209,7 @@ {/if}
{/if} - + {:else}
@@ -203,6 +238,8 @@
{feedbackString}
{/if} + +
Hello
diff --git a/src/app.css b/src/app.css index 25775c1..bdd8800 100644 --- a/src/app.css +++ b/src/app.css @@ -23,6 +23,44 @@ a:hover { color: #535bf2; } +.tooltip { + pointer-events: none; + position: absolute; + font-size: 18px; + text-align: center; + background: white; + padding: 10px 15px; + z-index: 5; + height: 30px; + line-height: 30px; + margin: 0 auto; + color: #21669e; + border-radius: 5px; + box-shadow: 0 0 0 1px #eee; + + -moz-transform: translateX(-50%); + -ms-transform: translateX(-50%); + -webkit-transform: translateX(-50%); + transform: translateX(-50%); + + display: none; + &.active { + display: block; + } + &:after { + content: ""; + position: absolute; + left: 50%; + top: 100%; + width: 0; + height: 0; + margin-left: -10px; + border-left: 10px solid transparent; + border-right: 10px solid transparent; + border-top: 10px solid white; + } +} + body { margin: 0; display: flex; -- cgit v1.3-2-g11bf