About
mTip is a flextendable jQuery tooltip plugin with a lot of customization options.
Please leave tips, suggestions and bug reports in the comment section belooowww.
Enjoy!
mTip is a flextendable jQuery tooltip plugin with a lot of customization options.
Please leave tips, suggestions and bug reports in the comment section belooowww.
Enjoy!
Move your mouse over the example texts to display the tooltip.
The plugin makes use of the black theme by default (don't forget to load the CSS).
<span id="tip-simple" title="This is a simple tooltip demonstration..">Tooltip simple</span>
$( '#tip-simple' ).mTip( {
align : 'right'
} );
<span class="demo" id="tip-html">Tooltip with HTML content</span>
$( "#tip-html" ).mTip( {
content : 'This <i>tooltip</i> has <strong>HTML</strong> content. Do you like the movie <u>Madagascar</u> too?<br />'
+ '<br /><img src="/library/mTip/madagascar.jpg" title="Madagascar" />',
align : 'bottom'
} );
<span id="tip-delay" title="This one shows on after half a second and hides after three.">Tooltip with delay</span>
$( '#tip-delay' ).mTip( {
delay : [500, 3000]
} );
<span id="tip-topleft" title="This one shows on mouse down.">Tooltip top left, shows on mousedown</span>
$( '#tip-topleft' ).mTip( {
showOn : 'mousedown',
align : 'top left'
} );
<span id="tip-content">Tooltip with dynamic content</span>
var counter = 0;
$( '#tip-content' ).mTip( {
align : 'bottom',
content : function() {
return 'This tooltip has been shown/updated ' + ++counter + ' time' + ( counter > 1 ? 's' : '' );
}
} );
<span id="tip-live" title="Fallback title">Tooltip with live content</span>
var $tip = $( '#tip-live' ).mTip().mTip( 'get' );
setInterval( function() {
$tip.html( ( new Date() ).toUTCString() );
}, 1000 );
<span id="tip-animate" title="Awesome!">Tooltip with custom animation</span>
$( '#tip-animate' ).mTip( {
show : function( $tip, options ) {
var offsetTop = parseInt( $tip.css( 'top' ) );
$tip.stop( true )
.css( 'top', 0 )
.show()
.animate( {
opacity : options.opacity,
top : offsetTop
}, options.fade[0], 'easeOutBounce' );
},
hide : function( $tip, options ) {
$tip.stop( true )
.animate( {
left : parseInt( $tip.css( 'left' ) ) + 100,
opacity : 0
}, options.fade[1], 'linear' );
},
align : 'right',
fade : [1000, 500]
} );
<span id="tip-box">Tooltip box, click me!</span>
$( '#tip-box' ).mTip( {
showOn : 'click',
hideOn : 'dummyHide', // Event will only be triggered by calling .mTip( 'hide' );
content : '<span onClick="javascript:$(\'#tip-box\').mTip(\'hide\');" title="Close"></span>'
+ 'This box will only close after clicking the close icon.',
align : 'right'
} );
$.fn.mTip.defaults.show = function( $tip, options ) {
if( $tip.css( 'opacity' ) == options.opacity ) return;
var offsetTop = parseInt( $tip.css( 'top' ) ),
offsetLeft = parseInt( $tip.css( 'left' ) ),
distance = 7000 / options.fade[0];
$tip.stop( true )
.css( {
top : ( ! options.align.left && ! options.align.right
? offsetTop + ( options.align.top ? -distance : 0 ) + ( options.align.bottom ? distance : 0 )
: offsetTop
),
left : offsetLeft + ( options.align.left ? -distance : 0 ) + ( options.align.right ? distance : 0 )
} )
.show()
.animate( {
opacity : options.opacity,
top : offsetTop,
left : offsetLeft
}, options.fade[0], options.fadeEasing[0] );
};
$.fn.mTip.defaults.hide = function( $tip, options ) {
var offsetTop = parseInt( $tip.css( 'top' ) ),
offsetLeft = parseInt( $tip.css( 'left' ) ),
distance = 7000 / options.fade[0];
$tip.stop( true )
.animate( {
top : ( ! options.align.left && ! options.align.right
? offsetTop + ( options.align.top ? -distance : 0 ) + ( options.align.bottom ? distance : 0 )
: offsetTop
),
left : offsetLeft + ( options.align.left ? -distance : 0 ) + ( options.align.right ? distance : 0 ),
opacity : 0
}, options.fade[1], options.fadeEasing[1], function() { $tip.hide() } );
};
Tooltip defaults can be found and modified in: $.fn.mTip.defaults.
The following methods are available:
| Methods | |||
| Code | Returns | Description | Since |
|---|---|---|---|
| .mTip( 'show' ) | Element(s), chainable | Show tooltip (calls first showOn event) | 1.0 |
| .mTip( 'hide' ) | Element(s), chainable | Hide tooltip (calls first hideOn event) | 1.0 |
| .mTip( 'update', 'content' ) | Element(s), chainable | Manually update tooltip (calls first updateOn event), and optionally update HTML content. |
1.0.1 |
| .mTip( 'get' ) | Tip element(s) | Get tooltip element(s) | 1.0.1 |
The following options are available:
| Events | ||||
| Key | Type | Default value | Description | Since |
|---|---|---|---|---|
| showOn | string | "mouseenter" | Show tooltip if this event is triggered | 1.0 |
| show | function | Show callback |
Function that fades in tooltip function( $tip, options, event ) { var $this = $(this); // Element } |
1.0 |
| beforeShow | function | Before show callback |
function( $tip, options, event ) { var $this = $(this); // Element return false; // Cancel showing tooltip } |
1.0.3 |
| hideOn | string | "mouseleave" | Hide tooltip if this event is triggered | 1.0 |
| hide | function | Hide callback |
Function that fades out tooltip function( $tip, options, event ) { var $this = $(this); // Element } |
1.0 |
| beforeHide | function | Before hide callback |
function( $tip, options, event ) { var $this = $(this); // Element return false; // Cancel hiding tooltip } |
1.0.3 |
| updateOn | string | "mousemove" | Update tooltip if this event is triggered | 1.0 |
| update | function | Update callback |
Function that updates tooltip position and data function( $tip, options, event ) { var $this = $(this); // Element } |
1.0 |
| beforeUpdate | function | Before update callback |
function( $tip, options, event ) { var $this = $(this); // Element return false; // Cancel updating tooltips position } |
1.0.3 |
| cancelUpdate | boolean | false | Force update cancelling | 1.0 |
| Note: The showOn, hideOn, and updateOn should not contain the same events! | ||||
| Content | ||||
| Key | Type | Default value | Description | Since |
| content | string/function | Elements "title" attribute value |
Content string or callback (for live content). |
1.0 |
| Note: mTip removes elements title attribute. | ||||
| Placement | ||||
| Key | Type | Default value | Description | Since |
| holder | string/object | "body" | jQuery selector or object | 1.0 |
| alignTo | string/object | "element" | Attach to cursor by setting this value to "cursor" or use a jQuery object | 1.0 |
| align | string | "bottom right" | Can be combinations of the words: "top" or "bottom" and "left" or "right" | 1.0 |
| spacing | integer/array | 5 | Value in pixels or array [top, left] | 1.0 |
| keepOnHover | boolean | true | Keep tooltip if mouse goes over tooltip | 1.0 |
| Timing | ||||
| Key | Type | Default value | Description | Since |
| delay | integer/array | 250 | Delay in milliseconds or array [show, hide] | 1.0 |
| fade | integer/array | 250 | Fade time in milliseconds or array [show, hide] | 1.0 |
| fadeEasing | string | "linear" | Fade easing | 1.0 |
| Styling | ||||
| Key | Type | Default value | Description | Since |
| className | string | "black" | These classes will be added, for styling purposes | 1.0 |
| opacity | double | 1 | Tooltips opacity | 1.0 |
| css | object | Object with default values | Tooltip css properties | 1.0 |
mTip is dual licensed under the MIT and GPL licenses.
It has been tested with jQuery version 1.6.2+ in IE7+, FF4+, Safari 5+, Chrome 8+, and Opera 11.
Other browsers and versions may work.
Notice: If you report a bug, please send me a testcase (via JSFiddle for example), this helps me a lot! Thanks.
blog comments powered by Disqus