JSFiddle - React, Tailwind, and code Playground

by trkli

HTML

<aside>
  <div class="single-ad">
    <div class="module module-hotjar" id="module-hotjar">
      <div class="moving-heatmap-area">
        <div class="click-area click-area-1"></div>
        <div class="click-area click-area-2"></div>
        <div class="click-area click-area-3"></div>
      </div>
    </div>
  </div>
</aside>

CSS

:root {
  --mouse-x: 100px;
  --mouse-y: 250px;
}

* {
  box-sizing: border-box;
}

aside {
  width: 300px;
  margin: 0 auto;
  padding: 1rem;
  display: flex;
  height: 100vh;
  flex-direction: column;
  justify-content: center;
}

body {
  margin: 0;
  background: #333;
  font: 12px "Lato", "Lucide Grande", sans-serif;
}

.module-hotjar {
  background: white;
  overflow: hidden;
  height: 300px;
  width: 100%;
  border: 1px solid orange;
}

.moving-heatmap-area {
  background: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/3/heat-map.png);
  width: 1024px;
  height: 440px;
  box-shadow: 0 0 20px rgba(0, 0, 0, 0.2);
  transform: translate(calc(calc(var(--mouse-x) / 1.5 * -1) + 120px), calc(calc(var(--mouse-y) / 2.5 * -1) + 20px));
}

.click-area {
  position: absolute;
}
.click-area:hover, .click-area:focus {
  border: 1px dashed #5B9FE5;
}
.click-area:hover::after, .click-area:focus::after {
  display: block;
}
.click-area::after {
  pointer-events: none;
  display: none;
  position: absolute;
  bottom: 100%;
  left: 0;
  background: #5B9FE5;
  color: white;
  padding: 0.1rem 0.3rem;
  white-space: nowrap;
}

.click-area-1 {
  left: 28px;
  top: 142px;
  width: 120px;
  height: 20px;
}
.click-area-1::after {
  content: "292 Clicks (39.46%)";
}

.click-area-2 {
  left: 28px;
  top: 162px;
  width: 140px;
  height: 20px;
}
.click-area-2::after {
  content: "187 Clicks (29.31%)";
}

.click-area-3 {
  left: 28px;
  top: 182px;
  width: 170px;
  height: 20px;
}
.click-area-3::after {
  content: "21 Clicks (4.12%)";
}

JavaScript

let root = document.documentElement;
let hotjarGraphic = document.getElementById("module-hotjar");

hotjarGraphic.addEventListener("mousemove", e => {
  console.log(e);
  root.style.setProperty('--mouse-x', e.pageX - hotjarGraphic.offsetLeft + "px");
  root.style.setProperty('--mouse-y', e.pageY - hotjarGraphic.offsetTop + "px");
});