-
Notifications
You must be signed in to change notification settings - Fork 15
Tooltip
var tip = new Tooltip(content, [options]);
Type: String|Element
Tooltip content. Can be a text, an HTML string, or a DOM element. Will be used as innerHTML
when a string, and .appendChild()
when an element.
If you are inserting image elements, they need to have their dimensions defined (either with width
& height
attributes, or in CSS), otherwise the Tooltip will work with dimensions before the image load. You can also bind a load event to the image, and when it triggers call .updateSize()
method.
Type: Object
Tooltip options object
Default options are stored in Tooltip.defaults
.
Tooltip.defaults = {
baseClass: 'tooltip', // Base tooltip class name.
typeClass: null, // Type tooltip class name.
effectClass: null, // Effect tooltip class name.
inClass: 'in', // Class used to transition stuff in.
place: 'top', // Default place.
spacing: null, // Gap between target and tooltip.
auto: 0 // Whether to automatically adjust place to fit into window.
};
Type String
Default tooltip
Base tooltip class that will be applied to tooltip element upon creation. This class should define tooltip's layout & dimensions, and shouldn't be changed.
Type String
Default null
Tooltip's type class that will be applied to tooltip element upon creation. This class should define only visual changes that don't affect layout & dimensions.
Can be changed dynamically with .type()
method.
Type String
Default null
Tooltip's effect class that will be applied to tooltip element upon creation. This class - in a correlation with inClass - should define only tooltip transitions. Example fade transition:
.tooltip.fade { opacity: 0; transition: opacity 200ms ease-out; }
.tooltip.fade.in { opacity: 1; transition-duration: 100ms; }
Can be changed dynamically with .effect()
method.
Type String
Default in
Tooltip's class for "transitioning in" state. It is applied to tooltip when it is being shown. This class doesn't have to declare anything, it functions mostly as a helper selector for effect classes.
Type String
Default top
This options specifies tooltip's growth direction. Tooltip supports this places:
-
top
,top-left
,top right
-
bottom
,bottom-left
,bottom-right
-
left
,left-top
,left-bottom
-
right
,right-top
,right-bottom
Type Integer
Default null
Defines the gap between tooltip and target position, used mainly to properly display tooltip arrow. This gap is defined with a top
property in tooltip's base class:
.tooltip {
top: 10px; /* Defines the spacing between tooltip and target position */
}
But can be overridden with this option.
Type Boolean
Default false
When set to true
, tooltip will check whether the current requested place is possible to display within window borders, and if not, it'll try to pick a better one.
Unless stated otherwise, all methods return tooltip object, making them chainable.
Change tooltip's content. If tooltip is visible, it'll automatically readjust it's dimensions and position.
-
content
String|Element
Can be a text, escaped HTML string, or DOM element.
Position the tooltip on specific coordinates.
Position the tooltip while using an element as position target.
Show the tooltip, and optionally position it. When arguments are present, they are directly relayed to .position()
method.
Hide tooltip.
Hide when shown, show when hidden.
Attach tooltip to DOM element. When tooltip is attached, .position()
arguments are ignored and attached element is always used as a tooltip target position. When attached tooltip is visible, it is automatically repositioned on window resize.
Detach tooltip from element.
Change type class. Pass falsy or no value to remove type class.
Change effect class. Pass falsy or no value to remove effect class.
The underlying method used by .type()
& .effect()
. For example, the .type()
method is basically just a:
Tooltip.prototype.type = function (name) {
return this.changeClassType('type', name);
};
You can use this to create your own class types.
Change default tooltip place. When tooltip is visible, it is automatically repositioned to the requested place.
Updates tip.width
& tip.height
properties to be in sync with the actual size of tooltip element. It is run on tooltip creation and automatically on .content()
changes.
You'd want to use this when you are inserting a DOM element as a tooltip content, and than manipulating this element from outside. Whenever some layout change happens to that element, .updateSize()
should be called.
Tooltip element. This element is detached from DOM when tooltip is hidden.
A component/classes
object handling tooltip element classes. You can do:
tip.classes.add('foo');
tip.classes.remove('foo');
tip.classes.toggle('foo');
tip.classes.has('foo');
tip.classes.array(); // returns an array of all current classes
Type Boolean
True when tooltip is hidden, false otherwise.
Tooltip options object.
Type Integer
Tooltip spacing - the additional gap between tooltip and it's target position. It is used to create a gap between tooltip and its target position so the tooltip arrow would not overlap the target. This value is a mirror of options.spacing
when defined, or retrieved from baseClass
's top
style otherwise. For example, to have a 10px gap between tooltip and its target:
.tooltip {
top: 10px;
}
You can also set it manually in options:
var tip = new Tooltip('foo', { spacing: 10 });
tip.show();
The purpose of the .tooltip:top
weirdness is to have all layout declarations in CSS and out of JavaScript. As the arrow size is declared 100% in CSS, so should be the gap needed for its proper display.
Width of tooltip element. Updated by .updateSize()
method.
Height of tooltip element. Updated by .updateSize()
method.
Element the tooltip is attached to, otherwise null
.