This is the simplest form of a mouseover "tooltip" UI that I could come up with.
You can see it used on local hyperlinks at https://deanebarker.net/.
Note: this is NOT a "library." This is starter code. There are no settings or configuration (meaning: everything is hard-coded) nor has it been designed to be extensible -- you are expected copy this code and change it. It's essentially just an example.
To use
- Call
bindTips
, passing a collection of elements. - Implement your own
getTipContent
. It will be passed the entire element that triggered the tip. Do whatever you want (read from an attribute, do a lookup on the HREF, whatever). Return the HTML content for the tip.
Example:
bindTips(document.querySelectorAll('a'));
function getTipContent(el) {
var href = el.getAttribute('href');
return href + " is a great URL!!!";
}
But I don't want to show tooltips on [insert criteria here]...
You control the collection passed to bindTips
. Form that collection however you want.
But I want them to look different...
There is literally one element you need to style. The CSS is right there. Go nuts.
But I just want to display the content of an attribute...
getTipContent
is passed the entire element. Do whatever you want.
<a href="whatever" data-tooltip="This is a really cool website"/>
function getTipContent(el) {
return el.getAttribute('data-tooltip');
}
But I want to retieve the tooltip from [insert hare-brained idea here]...
Again, do whatever you want in getTipContent
. It's designed to show some placeholder text until that function returns. I wouldn't dilly-dally or anything, but reasonable I/O isn't going to look weird (I do a background HTTP call in my use case; it feels fine to me).
Why don't you have a settings/config object?
Again, this is not a plugin/module/extension/library. I wrote this for me and my one and only use case. There will never be an automated update to this code via npm or yarn or whatever.
Copy the code, do whatever you want without fear.