I have a div tag as follows:
<html>
<head></head>
<body>
<div>
<label>Name</label>
<input type="text"/>
</div>
</body>
</html>
Now I want a simple javascript for displaying a tooltip on :hover the div. Can someone please help me out? The tooltip should also have a fade in/out effect.
It can be done with CSS only, no javascript at all : running demo
Apply a custom HTML attribute, eg.
tooltip="bla bla"
to your object (div or whatever):Define the
:before
pseudoelement of each[tooltip]
object to be transparent, absolutely positioned and withtooltip=""
value as content:Define
:hover:before
hovering state of each[tooltip]
to make it visible:Apply your styles (color, size, position etc) to the tooltip object; end of story.
In the demo I've defined another rule to specify if the tooltip must disappear when hovering over it but outside of the parent, with another custom attribute,
tooltip-persistent
, and a simple rule:Note 1: The browser coverage for this is very wide, but consider using a javascript fallback (if needed) for old IE.
Note 2: an enhancement may be adding a bit of javascript to calculate the mouse position and add it to the pseudo elements, by changing a class applied to it.
How about this , sorry code is not optimized cause im in a hurry , but i guess you will get the idea :
http://jsfiddle.net/prollygeek/1b0Lrr8d/
You don't need JavaScript for this at all; just set the
title
attribute:Note that the visual presentation of the tooltip is browser/OS dependent, so it might fade in and it might not. However, this is the semantic way to do tooltips, and it will work correctly with accessibility software like screen readers.
See:
I have developed three type fade effects :
Here's a nice jQuery Tooltip:
https://jqueryui.com/tooltip/
To implement this, just follow these steps:
Add this code in your
<head></head>
tags:On the HTML elements that you want to have the tooltip, just add a
title
attribute to it. Whatever text is in the title attribute will be in the tooltip.Note: When JavaScript is disabled, it will fallback to the default browser/operating system tooltip.
You can try bootstrap tooltips.
further reading here