-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathresponsive-tooltip.js
More file actions
75 lines (62 loc) · 2.43 KB
/
responsive-tooltip.js
File metadata and controls
75 lines (62 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
jQuery( document ).ready( function()
{
var targets = jQuery( '[rel~=tooltip]' ),
target = false,
tooltip = false,
title = false;
targets.bind( 'mouseenter', function()
{
target = jQuery( this );
tip = target.attr( 'title' );
tooltip = jQuery( '<div id="tooltip"></div>' );
if( !tip || tip == '' )
return false;
target.removeAttr( 'title' );
tooltip.css( 'opacity', 0 )
.html( tip )
.appendTo( 'body' );
var init_tooltip = function()
{
if( jQuery( window ).width() < tooltip.outerWidth() * 1.5 ) {
tooltip.css( 'max-width', jQuery( window ).width() / 2 );
} else {
tooltip.css( 'max-width', 340 );
}
var pos_left = target.offset().left + ( target.outerWidth() / 2 ) - ( tooltip.outerWidth() / 2 );
var pos_top = target.offset().top - jQuery(document).scrollTop() - tooltip.outerHeight() - 20;
if( pos_left < 0 ) {
pos_left = target.offset().left + target.outerWidth() / 2 - 20;
tooltip.addClass( 'left' );
} else {
tooltip.removeClass( 'left' );
}
if( pos_left + tooltip.outerWidth() > jQuery( window ).width() ) {
pos_left = target.offset().left - tooltip.outerWidth() + target.outerWidth() / 2 + 20;
tooltip.addClass( 'right' );
} else {
tooltip.removeClass( 'right');
}
if( pos_top < 0 ) {
var pos_top = target.offset().top + target.outerHeight();
tooltip.addClass( 'top' );
} else {
pos_top = target.offset().top - tooltip.outerHeight() - 20;
tooltip.removeClass( 'top' );
}
tooltip.css( { left: pos_left, top: pos_top } )
.animate( { top: '+=10', opacity: 1 }, 50 );
};
init_tooltip();
jQuery( window ).resize( init_tooltip );
var remove_tooltip = function()
{
tooltip.animate( { top: '-=10', opacity: 0 }, 50, function()
{
jQuery( this ).remove();
});
target.attr( 'title', tip );
};
target.bind( 'mouseleave', remove_tooltip );
tooltip.bind( 'click', remove_tooltip );
});
});