简体   繁体   English

如何使用单个 Vanilla JS 工具提示 div 显示 100 多个不同的工具提示?

[英]How to use a single Vanilla JS tooltip div to display 100+ different tooltips?

I've cooked up a HTML/JS website on which I want to show tooltips for icons like so:我已经制作了一个 HTML/JS 网站,我想在该网站上显示图标的工具提示,如下所示: 在此处输入图像描述

What I want to see:我想看到的:

  1. When I hover over nothing I want the div to be hidden in CSS.当我 hover 什么都不做时,我希望将 div 隐藏在 CSS 中。
  2. When I hover over another icon I want the div to get correct info.当我在另一个图标上 hover 时,我希望 div 获得正确的信息。

How I want to code it:我想如何编码:

Using a single top-level DIV like so:像这样使用单个顶级 DIV:

<div id="circle" class="circleTT">
<div id="circlecontent" class="civBoxTT">
  <h3><b>Villager (SAMPLE: NOT LIVE DATA)</b></h3>Gathers Wood, Food, Stone, and Gold. Builds and repairs buildings
  and can repair
  siege engines and naval units. - Weak in combat<br><img src="img/resourcefoodicon.png">50<br><img
    src="img/house.png">1<br><img src="img/timetobuild.png">20s<br><br>
</div>

Why?为什么? Because when using simpler tooltips had two problems: either I could not show images within the tooltip OR the tooltip would be inside the DIV and would add scrollbars to that part.因为当使用更简单的工具提示时有两个问题:要么我无法在工具提示中显示图像,要么工具提示将在 DIV 内并会在该部分添加滚动条。 Because the parent div is able to fold this was not fixable as far as my knowledge goes.因为父 div 能够折叠,据我所知这是无法修复的。

Then, adding mouseover in Javascript like so:然后,像这样在 Javascript 中添加鼠标悬停:

const isHover = e => e.parentElement.querySelector(':hover') === e;

  const myDiv = document.getElementById('tooltip');
  /*for (var i = 0; i < myDiv.length; i++) {*/
  document.addEventListener('mousemove', function checkHover() {
    const hovered = isHover(myDiv);
    if (hovered !== checkHover.hovered) {
      if (hovered) {
        document.getElementById('circlecontent').innerHTML = "<h3><b>Villager (age: 1)</b></h3>Gathers Wood, Food, Stone, and Gold. Builds and repairs buildings and can repair siege engines and naval units. - Weak in combat<br><img src=\"img/resourcefoodicon.png\">50<br><img src=\"img/house.png\">1<br><img src=\"img/timetobuild.png\">20s<br><br>";
      }
      else {
        document.getElementById('circlecontent').innerHTML = "";
      }
      checkHover.hovered = hovered;
    }
  });
  /*}*/

  let circle = document.getElementById('circle');

  const onMouseMove = (e) => {
    circle.style.left = e.pageX + 5 + 'px';
    circle.style.top = e.pageY + 5 + 'px';
  }

  document.addEventListener('mousemove', onMouseMove);

This is all from samples how other people made similar Tooltips这都是来自其他人如何制作类似工具提示的示例

However, when I simply apply this Mouseover Javascript to all elements then the webpage becomes extremely slow, probably because it is receiving the mouseover state 100-200 times per mouse-move.但是,当我简单地将鼠标悬停 Javascript 应用于所有元素时,网页会变得非常慢,可能是因为每次鼠标移动它接收鼠标悬停 state 100-200 次。

What is a good clean way to code this in simple Vanilla JS?用简单的 Vanilla JS 编写代码的好方法是什么?

After much more trial and error fixed it like so:经过更多的试验和错误修复它像这样:

///////////////////////////////////////////////////
  // SHOW tooltips
  ///////////////////////////////////////////////////
  let tooltipContainer = document.getElementById('tooltipContainer');
  const tooltipBox = document.getElementById('tooltipBox');
  document.addEventListener('mousemove', function checkHover(e) {
    tooltipContainer.style.left = getWidth() > e.pageX + 400 ? e.pageX + 2 + 'px' : e.pageX - 402 + 'px';
    tooltipContainer.style.top = e.pageY + 2 + 'px';
    const allTooltips = document.getElementsByClassName('tooltip');
    for (var i = 0; i < allTooltips.length; i++) {
      if (allTooltips.item(i).querySelector(':hover')) {
        tooltipBox.innerHTML = allTooltips.item(i).lastChild.innerHTML; tooltipBox.style.display = "block"; return;
      }
    }
    tooltipBox.innerHTML = ""; tooltipBox.style.display = "none";
  });

Basically every time the mouse moves there is a single MouseMove event that checks the mouse position, adjusts the position of the DIV, and finally runs through all elements with tooltip to check if any is hovered.基本上每次鼠标移动时都会有一个 MouseMove 事件检查鼠标 position,调整 DIV 的 position,最后使用工具提示遍历所有元素以检查是否有悬停。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM