forked from phallguy/jquery-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.poppable.js
64 lines (57 loc) · 1.74 KB
/
jquery.poppable.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
/*
* Poppable
* Copyright 2011 Apps In Your Pants Corporation
* http://github.com/appsinyourpants/jquery-plugins
*
* Version 1.0 - Updated: Jan. 21, 2010
*
* This jQuery plug-in is lcensed under a Creative Commons Attribution 3.0 Unported License. http://creativecommons.org/licenses/by/3.0/
*/
(function($) {
$.fn.all = function(selector) {
var children = $(selector, this);
var self = this.filter(selector);
var combined = children.add(self);
return combined;
}
$.fn.poppable = function() {
this.each(function() {
var pop = $(this);
pop.all(".trigger").each(function() {
var trigger = $(this);
var target = trigger.attr('popTarget');
var popup = target ? $(target, target.indexOf('#') == 0 ? $('body') : pop) : $(".popup", pop);
var hideTimer = null;
var hasFocus = false;
popup.hide();
var hide = function() {
hideTimer = setTimeout(function() {
hideTimer = null;
popup.fadeOut();
}, 300);
}
$('input,textarea', popup).focus(function() { hasFocus = true });
$('input,textarea', popup).blur(function() { hasFocus = false; hide(); });
$($.makeArray(trigger).concat($.makeArray(popup)))
.mouseover(function() {
if (hideTimer) clearTimeout(hideTimer);
var parent = trigger.offsetParent();
popup.css({
left: trigger.offset().left - parent.offset().left - (parent.outerWidth() - parent.width()) / 4,
top: trigger.offset().top - parent.offset().top + trigger.outerHeight() - 1,
position: "absolute"
});
popup.show();
return false;
})
.mouseout(function() {
if (hideTimer) clearTimeout(hideTimer);
if (!hasFocus)
hide();
return false;
});
});
});
return this;
}
})(jQuery);