-
Notifications
You must be signed in to change notification settings - Fork 1
/
jquery.codabubble.js
155 lines (130 loc) · 5.45 KB
/
jquery.codabubble.js
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*!
* jQuery Codabubble Plugin
* http://github.com/elidupuis
*
* Copyright 2010, Eli Dupuis
* Version: 0.4
* Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) and GPL (http://creativecommons.org/licenses/GPL/2.0/) licenses.
* Requires: jQuery v1.4.2 or later
* Based heavily on Remy Sharp's snippet at http://jqueryfordesigners.com/coda-popup-bubbles/
TODO:
- destroy function
- add smart-direction feature? if direction is set to RIGHT and there's no room for element, make it appear on the left.
*/
(function($) {
var ver = '0.4',
methods = {
init: function( options ) {
// iterate and reformat each matched element
return this.each(function() {
var $this = $(this),
opts = $.extend({}, $.fn.codabubble.defaults, options),
data = $this.data('codabubble');
// If the plugin hasn't been initialized yet
if ( ! data ) {
var hideDelayTimer = null,
beingShown = false, // tracker
shown = false,
trigger = $(opts.triggerClass, this),
popup = $(opts.popupClass, this).css('opacity', 0),
offset,
defaultCss = {},
directionProperty;
// determine offset format:
if ( typeof opts.offset === 'function' ) {
offset = opts.offset.call(this, popup, $this) + 'px';
}else{
offset = opts.offset + 'px';
};
// determine desired direction:
switch ( opts.direction ) {
case 'left' :
directionProperty = 'left';
break;
case 'right' :
directionProperty = 'right';
break;
case 'down' :
directionProperty = 'bottom';
break;
default :
directionProperty = 'top';
break;
};
// setup default css based on desired direction:
defaultCss[directionProperty] = offset;
defaultCss['display'] = 'block';
// attach mouseover/mouseout listeners and functionality:
$([trigger.get(0), popup.get(0)]).bind({
'mouseover.codabubble': function () {
// stops the hide event if we move from the trigger to the popup element
if (hideDelayTimer) {
clearTimeout(hideDelayTimer);
};
// don't trigger the animation again if we're being shown, or already visible
if (beingShown || shown) {
return;
} else {
beingShown = true;
// setup animation properties
var animCSS = { opacity: 1 };
animCSS[directionProperty] = '-=' + opts.distance + 'px';
// reset position of popup box and animate:
popup.css( defaultCss ).animate(animCSS, opts.time, 'swing', function() {
// once the animation is complete, set the tracker variables
beingShown = false;
shown = true;
});
}
},
'mouseout.codabubble': function () {
// reset the timer if we get fired again - avoids double animations
if (hideDelayTimer) {
clearTimeout(hideDelayTimer);
};
// store the timer so that it can be cleared in the mouseover if required
hideDelayTimer = setTimeout(function () {
hideDelayTimer = null;
var animCSS = { opacity: 0 };
animCSS[directionProperty] = '-=' + opts.distance + 'px';
popup.animate(animCSS, opts.time, 'swing', function () {
shown = false; // once the animate is complete, set the tracker variables
popup.hide(); // hide the popup entirely after the effect (opacity alone doesn't do the job)
});
}, opts.hideDelay);
}
}).trigger('mouseout.codabubble');
// attach data:
$(this).data('codabubble', {
target : $this,
opts: opts
});
};
});
},
destroy: function() {
// to be implemented....
if(window.console) window.console.log('destroy called.');
}
};
$.fn.codabubble = function( method ) {
if ( methods[method] ) {
return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
} else if ( typeof method === 'object' || ! method ) {
return methods.init.apply( this, arguments );
} else {
$.error( 'Method ' + method + ' does not exist on jQuery.codabubble' );
};
};
// defaults
$.fn.codabubble.defaults = {
distance: 10, // distance traveled by bubble during animation.
offset: 0, // offset distance. either an integer of a function that returns an integer
time: 250, // milliseconds. duration of the animation.
hideDelay: 500, // milliseconds. time before bubble fades out (after mouseout)
direction: 'up', // either 'left', 'right', down' or 'up'
triggerClass: '.trigger', // class of the trigger (in your markup)
popupClass: '.popup' // class of the bubble (in your markup)
};
$.fn.codabubble.ver = function() { return "jquery.codabubble ver. " + ver; };
})(jQuery);