basic idea of a custom tooltip, using pure Javascr

2019-02-13 21:06发布

问题:

I need basic idea of creating a custom tooltip using pure Javascript code;

What I want :

For example :

<a href="#" onmousemover="tooltip('text')">Link Text</a>

And onmouseover I want to display a custom tooltip with fixed position based on <a>'s element position, to start from right:0 or from left:0 of the <a> element;

回答1:

As this question comes up when a user searches for custom tooltip, I would like to add the bootstrap's JS tooltip, although you have mentioned pure javascript. So in a sense this answer is only to give the future readers another nice option to work with.

Take a look here for how to use bootstrap tooltip plugin. And here to learn about the options and methods available at your disposal when using Bootstrap's JS Tooltip.

Still I am giving the tryit from their site here:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
  <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
  <style>
  /* Tooltip */
  .test + .tooltip > .tooltip-inner {
      background-color: #73AD21; 
      color: #FFFFFF; 
      border: 1px solid green; 
      padding: 15px;
      font-size: 20px;
  }
  /* Tooltip on top */
  .test + .tooltip.top > .tooltip-arrow {
      border-top: 5px solid green;
  }
  /* Tooltip on bottom */
  .test + .tooltip.bottom > .tooltip-arrow {
      border-bottom: 5px solid blue;
  }
  /* Tooltip on left */
  .test + .tooltip.left > .tooltip-arrow {
      border-left: 5px solid red;
  }
  /* Tooltip on right */
  .test + .tooltip.right > .tooltip-arrow {
      border-right: 5px solid black;
  }
  </style>
</head>
<body>

<div class="container">
  <h3>Tooltip CSS Example</h3>
  <ul class="list-inline">
    <li><a class="test" href="#" data-toggle="tooltip" data-placement="top" title="Hooray!">Top</a></li>
    <li><a class="test" href="#" data-toggle="tooltip" data-placement="bottom" title="Hooray!">Bottom</a></li>
    <li><a class="test" href="#" data-toggle="tooltip" data-placement="left" title="Hooray!">Left</a></li>
    <li><a class="test" href="#" data-toggle="tooltip" data-placement="right" title="Hooray!">Right</a></li>
  </ul>
</div>

<script>
$(document).ready(function(){
    $('[data-toggle="tooltip"]').tooltip();   
});
</script>

</body>
</html>

P.S.: Before you downvote for this answer not being an answer to the OP, please note that I intended this to be of help to future readers only, since a search for custom tooltip leads here.



回答2:

I have a great idea about this question.

HTML

<a href="https://www.google.com" tooltip="Go to Google">Google</a>

JavaScript

(function () {

var a = document.getElementsByTagName('*'),
    tip, text,
    base = document.createElement('tooltip'); //Defining all objects

for (var x=0;x < a.length;x++) { //I'm using "for" loop to get all "a" elements with attribute "tooltip".
     a[x].onmouseover = function () {
         text = this.getAttribute('tooltip');
         tip = document.createTextNode(text);
         if (text != null) {// Checking if tooltip is empty or not.
               base.innerHTML = '';
               base.appendChild(tip);
               if (document.getElementsByTagName('tooltip')[0]){// Checking for any "tooltip" element
                   document.getElementsByTagName('tooltip')[0].remove();// Removing old tooltip
               }
               base.style.top = (event.pageY + 20) + 'px';
               base.style.left = (event.pageX + 20) + 'px';
               document.body.appendChild(base);
         }

     };
     a[x].onmouseout = function () {
         document.getElementsByTagName('tooltip')[0].remove();// Remove last tooltip
     };
}

})();

By including this script into your page, you just have to use tooltip="Text" in any HTML Element

CSS

tooltip {
    position: fixed;
    background: #fff;
    color: #333;
    border: 2px solid #333;
    font-size: 15px;
}

You can edit CSS as your wish!

Here's my JSFiddle

Hope this will help you.



回答3:

You need a function something like this:

function tooltip(text, element) {

    var offsetDistance = 20;

    tt = document.getElementById('ttdiv');
    elem = document.getElementById(element.id);
    tt.style.top = elem.offsetTop + offsetDistance + 'px';
    tt.style.left = elem.offsetLeft + offsetDistance + 'px';
    tt.style.display = 'block';
    tt.innerHTML = text;
}

Where ttdiv is the id of the tooltip div, that is initially set to display:none. You'll need an accompanying hide() function to hide the tooltip when onmouseout is triggered.

Here's a working JSFiddle.