forked from soundasleep/jquery-dropdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.dropdown.js
79 lines (63 loc) · 2.01 KB
/
jquery.dropdown.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
/*
* jQuery dropdown: A simple dropdown plugin
*
* Inspired by Bootstrap: http://twitter.github.com/bootstrap/javascript.html#dropdowns
*
* Copyright 2011 Cory LaViska for A Beautiful Site, LLC. (http://abeautifulsite.net/)
*
* Dual licensed under the MIT or GPL Version 2 licenses
*
*/
if(jQuery) (function($) {
$.extend($.fn, {
dropdown: function(method, data) {
switch( method ) {
case 'hide':
hideDropdowns();
return $(this);
case 'attach':
return $(this).attr('data-dropdown', data);
case 'detach':
hideDropdowns();
return $(this).removeAttr('data-dropdown');
case 'disable':
return $(this).addClass('dropdown-disabled');
case 'enable':
hideDropdowns();
return $(this).removeClass('dropdown-disabled');
}
}
});
function showMenu(event) {
var trigger = $(this),
dropdown = $( $(this).attr('data-dropdown') ),
isOpen = trigger.hasClass('dropdown-open');
event.preventDefault();
event.stopPropagation();
hideDropdowns();
if( isOpen || trigger.hasClass('dropdown-disabled') ) return;
dropdown
.css({
left: dropdown.hasClass('anchor-right') ?
trigger.offset().left - (dropdown.outerWidth() - trigger.outerWidth()) : trigger.offset().left,
top: trigger.offset().top + trigger.outerHeight()
})
.show();
trigger.addClass('dropdown-open');
};
function hideDropdowns(event) {
var targetGroup = event ? $(event.target).parents().andSelf() : null;
if( targetGroup && targetGroup.is('.dropdown-menu') && !targetGroup.is('A') ) return;
$('BODY')
.find('.dropdown-menu').hide().end()
.find('[data-dropdown]').removeClass('dropdown-open');
};
$(function () {
$('BODY').on('click.dropdown', '[data-dropdown]', showMenu);
$('HTML').on('click.dropdown', hideDropdowns);
// Hide on resize (IE7/8 trigger this when any element is resized...)
if( !$.browser.msie || ($.browser.msie && $.browser.version >= 9) ) {
$(window).on('resize.dropdown', hideDropdowns);
}
});
})(jQuery);