Skip to content

Commit cd8ccfc

Browse files
committed
first release
1 parent 06849b5 commit cd8ccfc

File tree

2 files changed

+225
-0
lines changed

2 files changed

+225
-0
lines changed

bower.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "tinytip",
3+
"version": "0.0.1",
4+
"homepage": "http://sweefty.com/tinytip",
5+
"authors": [
6+
"mamod <mamod.mehyar@gmail.com>"
7+
],
8+
"description": "Tiny Tooltip jQuery Plugin",
9+
"main": "tinytip.js",
10+
"keywords": [
11+
"tooltip",
12+
"tiny"
13+
],
14+
"license": "MIT",
15+
"ignore": [
16+
"**/.*",
17+
"node_modules",
18+
"bower_components",
19+
"test",
20+
"tests"
21+
]
22+
}

tinytip.js

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
/*
2+
* heat.js
3+
* MIT Licensed
4+
* @author Mamod Mehyar
5+
* http://sweefty.com
6+
* version : 0.0.1
7+
*/
8+
9+
(function($){
10+
"use strict";
11+
var _preventOnMouseEnter = false;
12+
var _SW_tooltip,_SW_tooltipClass;
13+
$(document).ready(function(){
14+
//initiate tooltip div
15+
_SW_tooltip = $('<div class="tinytip"></div>').css({
16+
width : 'auto',
17+
'max-width' : '90%',
18+
position : 'absolute',
19+
display : 'none',
20+
top : '0',
21+
left : '0',
22+
margin: '0',
23+
zIndex : '99999'
24+
}).appendTo('body');
25+
26+
_SW_tooltip.on('mouseenter', function(){
27+
_preventOnMouseEnter = true;
28+
}).on('mouseleave', function(){
29+
_preventOnMouseEnter = false;
30+
});
31+
});
32+
33+
var tooltip_options = {
34+
position : 'top',
35+
speed : 250,
36+
easing : 'linear',
37+
on: 'mouseenter',
38+
off : 'mouseleave'
39+
};
40+
41+
var lastElement;
42+
var _isview = false;
43+
44+
var _tooltip = function(ele,obj){
45+
obj = $.extend({},tooltip_options, obj);
46+
var text = obj.tooltip || ele.data('tooltip');
47+
var fix = obj.fix || '0 0';
48+
49+
if (text || obj.content){
50+
var fixcoords = fix.split(' '),
51+
fixX = parseInt(fixcoords[0]),
52+
fixY = parseInt(fixcoords[1]);
53+
var _class = obj.addClass;
54+
var position = obj.position || 'top';
55+
var speed = parseInt(obj.speed);
56+
var startX = function(x){
57+
return x;
58+
}, startY = function(y){
59+
return y;
60+
};
61+
62+
if (obj.animation){
63+
var xy = obj.animation.split('+');
64+
if (xy.length == 2){
65+
xy.push('+');
66+
} else {
67+
xy = [];
68+
xy = obj.animation.split('-');
69+
xy.push('-');
70+
}
71+
72+
var val = parseInt(xy[1]);
73+
if (xy[0] == 'left'){
74+
startX = function(x){
75+
//var val = parseInt(xy[1]);
76+
var newx = (xy[2] == '-') ? x-val : x+val;
77+
return newx;
78+
};
79+
} else {
80+
startY = function(y){
81+
var newy = (xy[2] == '-') ? y-val : y+val;
82+
return newy;
83+
};
84+
}
85+
}
86+
87+
var start1,start2;
88+
var closeTooltip = function(e){
89+
e.preventDefault();
90+
setTimeout(function(){
91+
if (_preventOnMouseEnter && obj.preventClose){
92+
closeTooltip(e);
93+
return;
94+
}
95+
96+
if (!_isview){ return false; }
97+
98+
//e.preventDefault();
99+
_SW_tooltip.stop().animate({
100+
opacity: 0,
101+
top:start1,
102+
left:start2
103+
}, speed, obj.easing, function(){
104+
$(this).hide();
105+
_isview = false;
106+
if (obj.onClose && typeof obj.onClose === 'function'){
107+
obj.onClose();
108+
}
109+
if (obj.clone){
110+
obj.clone.remove();
111+
}
112+
});
113+
}, 60);
114+
};
115+
116+
var viewTooltip = function(e){
117+
if (e){ e.preventDefault(); }
118+
if (_isview && obj.on === obj.off){
119+
if (lastElement === ele){
120+
closeTooltip(e);
121+
return;
122+
} else {
123+
_isview = false;
124+
}
125+
} else if (_isview && lastElement === ele){
126+
return;
127+
}
128+
129+
lastElement = ele;
130+
if (_SW_tooltipClass){
131+
_SW_tooltip.removeClass(_SW_tooltipClass);
132+
}
133+
134+
_SW_tooltipClass = _class;
135+
if (obj.content){
136+
obj.clone = obj.content.clone();
137+
_SW_tooltip.html(obj.clone);
138+
obj.clone.show();
139+
} else if (text){
140+
_SW_tooltip.html(text);
141+
}
142+
143+
var top = ele.offset().top + fixY;
144+
var left = ele.offset().left + fixX;
145+
var width = ele.outerWidth();
146+
var height = _SW_tooltip.outerHeight();
147+
148+
if (position == 'bottom'){
149+
top = top + ele.outerHeight() + 2;
150+
left = left + (width/2);
151+
left = left - (_SW_tooltip.outerWidth()/2);
152+
} else if (position == 'left'){
153+
left = left - _SW_tooltip.outerWidth() - 2;
154+
} else if (position == 'right'){
155+
left = left + width + 2;
156+
} else {
157+
left = left + (width/2);
158+
left = left - (_SW_tooltip.outerWidth()/2);
159+
top = top - height - 2;
160+
}
161+
162+
start1 = parseFloat(startY(top));
163+
start2 = parseFloat(startX(left));
164+
_SW_tooltip.addClass(_class).stop().css({
165+
top : start1,
166+
left : start2,
167+
opacity: 0,
168+
display : ''
169+
}).animate({ opacity: 1, top:top, left:left}, speed, obj.easing, function(){
170+
_isview = true;
171+
if (obj.onLoad && typeof obj.onLoad === 'function'){
172+
obj.onLoad(obj.clone || _SW_tooltip.find('p'));
173+
}
174+
});
175+
};
176+
177+
ele.on(obj.on, function(e){
178+
viewTooltip(e);
179+
}).on( obj.off, function(e){
180+
closeTooltip(e);
181+
});
182+
}
183+
};
184+
185+
//jQuery custom functions
186+
$.fn.tinytip = function(text, customOptions) {
187+
if (text && typeof text === 'object'){
188+
customOptions = text;
189+
text = customOptions.tooltip;
190+
}
191+
192+
var options = $.extend({}, tooltip_options, customOptions);
193+
options.tooltip = text;
194+
return this.each(function(){
195+
var $this = $(this);
196+
if ($this[0] == $(document)[0]){
197+
tooltip_options = $.extend({}, tooltip_options, text);
198+
} else {
199+
_tooltip($this, options);
200+
}
201+
});
202+
};
203+
}(jQuery));

0 commit comments

Comments
 (0)