VadimFr

Tooltip

Jun 7th, 2021 (edited)
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function onShow(e){
  2.       const elem = e.toElement;
  3.       const rect = elem.getBoundingClientRect();
  4.       var tipId = elem.getAttribute('data-tooltip');
  5.       elem.setAttribute('data-tip-id', tipId);
  6.       var tip = document.createElement("div");
  7.       tip.setAttribute('id', tipId);
  8.       tip.innerHTML = elem.getAttribute('data-tooltip');
  9.       if ((document.documentElement.clientHeight - rect.bottom - 50) > 0) {
  10.         tip.style.top = rect.bottom + 10 + 'px';
  11.       } else {
  12.         tip.style.top = rect.bottom - 60 + 'px';
  13.       }
  14.       tip.style.width = 200 + 'px';    
  15.       tip.setAttribute('class','tooltip tooltip_active');
  16.       document.body.appendChild(tip);
  17.     }
  18.  
  19.     function onHide(){
  20.       const elem = document.querySelector('.tooltip');
  21.        elem.parentNode.removeChild(elem);
  22.       elem.style.display = 'none';
  23.     }
  24.  
  25. function detach (element){
  26.   element.removeEventListener("mouseover", onShow);
  27.    element.removeEventListener("mouseout", onHide);
  28. }
  29.  
  30.     function showTooltips(){
  31.         var elems = document.querySelectorAll('span');
  32.       elems.forEach(function(elem) {
  33.         elem.addEventListener("mouseover", onShow);
  34.         elem.addEventListener("mouseout", onHide);
  35.     });
  36.     }
  37.  
  38.  
  39.  
  40.     window.onload = function(){
  41.         showTooltips();
  42.     }
Add Comment
Please, Sign In to add comment