forked from JoshuaD84/dhara-theme
-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu.js
133 lines (111 loc) · 4.55 KB
/
menu.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
jQuery(document).ready(function() {
// Mobile Site Menu - Nav Toggle
jQuery(".mobile-nav-toggle").click(function(e) {
e.preventDefault();
jQuery(this).toggleClass("active-mobile-menu");
jQuery(".nav").toggle();
jQuery("#mobile-search").hide();
jQuery(".mobile-search-toggle").removeClass("active-mobile-menu");
jQuery("#mobile-login").hide();
jQuery(".mobile-login-toggle").removeClass("active-mobile-menu");
});
// Mobile Site Menu - Search Toggle
jQuery(".mobile-search-toggle").click(function(e) {
e.preventDefault();
if( jQuery("#mobile-search").is(":visible") ) {
jQuery("#mobile-search").hide();
jQuery(".mobile-search-toggle").removeClass("active-mobile-menu");
} else {
jQuery("#mobile-search").show();
jQuery(".mobile-search-toggle").addClass("active-mobile-menu");
jQuery("#mobile-login").hide();
jQuery(".mobile-login-toggle").removeClass("active-mobile-menu");
jQuery(".nav").hide();
jQuery(".mobile-nav-toggle").removeClass("active-mobile-menu");
}
});
// Mobile Site Menu - Login Toggle
jQuery(".mobile-login-toggle").click(function(e) {
e.preventDefault();
if( jQuery("#mobile-login").is(":visible") ) {
jQuery("#mobile-login").hide();
jQuery(".mobile-login-toggle").removeClass("active-mobile-menu");
} else {
jQuery("#mobile-login").show();
jQuery(".mobile-login-toggle").addClass("active-mobile-menu");
jQuery("#mobile-search").hide();
jQuery(".mobile-search-toggle").removeClass("active-mobile-menu");
jQuery(".nav").hide();
jQuery(".mobile-nav-toggle").removeClass("active-mobile-menu");
}
});
adjustMenu();
//Edge detection for first-level drop down menus
jQuery(".nav > li > a").on('mouseenter', function (e) {
if ( !jQuery("#mobile-menu").is(':visible') ) {
var subMenu = jQuery('ul:first', jQuery(this).parent() );
if ( subMenu.length ) { /* If we have a submenu */
subMenu.removeClass('edge');
var subMenuRight = subMenu.width() + subMenu.offset().left;
var bodyRight = jQuery("body").offset().left + jQuery("body").width();
if ( subMenuRight > bodyRight) {
subMenu.addClass('edge');
}
}
}
});
//Edge detection for second-level drop down menus
jQuery(".nav ul > .menu-item-has-children").on('mouseenter', function (e) {
if ( !jQuery("#mobile-menu").is(':visible') ) {
var subMenu = jQuery('ul:first', this );
subMenu.removeClass("kick-left");
var subMenuRight = subMenu.offset().left + subMenu.width();
var bodyRight = jQuery("body").offset().left + jQuery("body").width();
if ( subMenuRight > bodyRight ) {
subMenu.addClass('kick-left');
}
}
});
//Touch Device Menu Handling
var isTouchDevice = false;
jQuery ( "html" ) .on ( 'touchstart', function ( e ) { isTouchDevice = true; } );
//It would be nice to do this, but ipads trigger mousemove events, so we can't
//jQuery ( "html" ) .on ( 'mousemove', function ( e ) { isTouchDevice = false; } );
//2017/01/25 - JDH - I'd like to have it go through the link if the item is clicked twice
//But it's not too important. All functionality exists, and this is nice and simple
jQuery ( ".menu-item-has-children > a" ) . on ( 'click', function ( e ) {
if ( ! isTouchDevice ) return true;
return false; //Prevents default behavior, i.e. going to the link
} );
})
/* Hide or Show the mobile menu bar, depending on page size */
jQuery(window).bind('resize orientationchange', function() {
adjustMenu();
});
/* The function that hides or shows the mobile menu */
var adjustMenu = function() {
if ( jQuery("#mobile-menu").is(':visible') ) {
if (jQuery(".mobile-nav-toggle").hasClass("active-mobile-menu")) {
jQuery(".nav").show();
} else {
jQuery(".nav").hide();
}
jQuery(".nav .menu-item-has-children > a").unbind('click').bind('click', function(e) {
// must be attached to anchor element to prevent bubbling
e.preventDefault();
jQuery(this).parent("li").toggleClass("show-sub-menu");
});
} else {
jQuery("#mobile-search").hide();
jQuery("#mobile-login").hide();
jQuery(".nav").show();
jQuery(".mobile-nav-toggle").removeClass("active-mobile-menu");
jQuery(".nav li a").unbind('click');
/*This causes the menu kick to stop functioning after resizing the screen
jQuery(".nav li").unbind('mouseenter mouseleave').bind('mouseenter mouseleave', function() {
// must be attached to li so that mouseleave is not triggered when hover over submenu
jQuery(this).toggleClass('hover');
});
*/
}
}