forked from tessalt/dropdowns
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dropdowns.js
63 lines (56 loc) · 1.6 KB
/
dropdowns.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
(function($){
$.fn.dropdowns = function (options) {
var defaults = {
toggleWidth: 768
}
var options = $.extend(defaults, options);
var ww = document.body.clientWidth;
var addParents = function() {
$(".nav li a").each(function() {
if ($(this).next().length > 0) {
$(this).addClass("parent");
}
});
}
var adjustMenu = function() {
if (ww < options.toggleWidth) {
$(".toggleMenu").css("display", "inline-block");
if (!$(".toggleMenu").hasClass("active")) {
$(".nav").hide();
} else {
$(".nav").show();
}
$(".nav li").unbind('mouseenter mouseleave');
$(".nav li a.parent").unbind('click').bind('click', function(e) {
// must be attached to anchor element to prevent bubbling
e.preventDefault();
$(this).parent("li").toggleClass("hover");
});
}
else if (ww >= options.toggleWidth) {
$(".toggleMenu").css("display", "none");
$(".nav").show();
$(".nav li").removeClass("hover");
$(".nav li a").unbind('click');
$(".nav li").unbind('mouseenter mouseleave').bind('mouseenter mouseleave', function() {
// must be attached to li so that mouseleave is not triggered when hover over submenu
$(this).toggleClass('hover');
});
}
}
return this.each(function() {
$(".toggleMenu").click(function(e) {
e.preventDefault();
$(this).toggleClass("active");
$(this).next(".nav").toggle();
adjustMenu();
});
adjustMenu();
addParents();
$(window).bind('resize orientationchange', function() {
ww = document.body.clientWidth;
adjustMenu();
});
});
}
})(jQuery)