-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathjquery.mobile-menu.js
196 lines (166 loc) · 5.71 KB
/
jquery.mobile-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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/* Mobile Menu jQuery Plugin
*
* Creates a side nav bar that mimics the native IOS nav slide drawer
*
* Author: Nick Brewer
* Version: 0.5
*
* REQUIRES: jQuery
*/
var mobileApp = mobileApp || {};
;(function(mobileApp,$){
var MobileMenu = function(elem, options){
this.elem = elem;
this.$elem = $(elem);
this.options = options;
};
MobileMenu.prototype = {
defaults: {
page_id: 'build-menu-page',
menu: '', // set as an array for multiple menus
menu_width: 260,
menu_id: "mobile-nav",
button_content: 'MENU',
prepend_button_to: '',
menu_bar: ''
},
/*
* Initiate app. Set Layout.
*
* @return this
*/
init: function(){
var _this = this;
_this.config = $.extend({}, _this.defaults, _this.options);
if($(_this.config.menu_id).length === 0){
this.setLayout();
}
return _this;
},
/*
* Clone site navigation and set it as mobile nav, set Class on each new menu
*
* @return false if no menu option is provided
*/
buildMenu: function(){
var _this = this;
_this.config = $.extend({}, _this.defaults, _this.options);
var menu = _this.config.menu,
mobile_menu = $("#build-menu"),
menu_collection = [];
// GET MENU AND BUILD MOBILE NAV
if(menu){
if($.isArray(menu)){
$(menu).each(function(i, e){
mobile_menu.append($(e).clone().addClass(_this.config.menu_id+"-"+i));
$(e).hide();
});
} else {
mobile_menu.append($(menu).clone().addClass(_this.config.menu_id+"-0").removeAttr("id"));
$(menu).hide();
}
} else {
return false;
}
},
/*
* Set CSS for new layout.
*
* @return void
*/
setCSS: function(){
var _this = this;
_this.config = $.extend({}, _this.defaults, _this.options);
$("#build-menu-overlay").css({
position: "absolute",
top: 0,
bottom: 0,
right: 0,
left: 0,
"z-index": 99,
background: "#000",
opacity: 0.5,
display: "none"
});
$("html,body").css("height","100%");
if(_this.config.menu_bar){
$(_this.config.menu_bar).css({
position: "fixed"
});
}
//$("<style />").appendTo("head").html('#'+_this.config.page_id+' { position: relative; min-height: 100% }');
//$("style").append('html.build-menu-open #'+_this.config.page_id+' { position: fixed; overflow: hidden; width: 100%; left: 0; top: 0; bottom: 0 }');
},
/*
* Set Layout, Build Menu, Set CSS. Set event handler for menu.
*
* @return void
*/
setLayout: function(){
var _this = this;
_this.config = $.extend({}, _this.defaults, _this.options);
// If prepend_button_to is not set to something custom, then just prepend to the page setting
if(_this.config.prepend_button_to == ''){
var prepend_button_to = "#"+_this.config.page_id;
} else {
var prepend_button_to = _this.config.prepend_button_to;
}
// SET HTML FRAMEWORK
_this.$elem.wrapInner('<div id="'+_this.config.page_id+'" />').find("#"+_this.config.page_id).before('<div id="build-menu" />');
$(prepend_button_to).prepend('<a href="#" id="build-menu-button">'+_this.config.button_content+'</a>');
$("#"+_this.config.page_id).prepend('<div id="build-menu-overlay" />');
this.buildMenu();
this.setCSS();
var element = document.getElementById(_this.config.page_id);
element.addEventListener("oTransitionEnd", remove_animation_class,false);
element.addEventListener("transitionend", remove_animation_class,false);
element.addEventListener("webkitTransitionEnd", remove_animation_class,false);
element.addEventListener("MSTransitionEnd", remove_animation_class,false);
function remove_animation_class(){
if($("html").hasClass("build-menu-close")){
$("html").removeClass("build-menu-animating");
}
}
// EVENT HANDLER FOR MENU BUTTON
$("#build-menu-button, #build-menu-overlay").on("click", function(e){
e.preventDefault();
var html = $("html");
var page = $("#"+_this.config.page_id);
var overlay = $("#build-menu-overlay");
html.addClass("build-menu-animating");
if(html.hasClass("build-menu-open")){
html.removeClass("build-menu-open");
html.addClass("build-menu-close");
page.css({
"-webkit-transform": "translateX(0px)",
"-moz-transform": "translateX(0px)",
"-o-transform": "translateX(0px)",
"-ms-transform": "translateX(0px)",
"transform": "translateX(0px)"
});
overlay.fadeTo("slow",0, function(){
$(this).css("visibility", "hidden");
});
} else {
html.addClass("build-menu-open");
html.removeClass("build-menu-close");
page.css({
"-webkit-transform": "translateX("+_this.config.menu_width+"px"+")",
"-moz-transform": "translateX("+_this.config.menu_width+"px"+")",
"-o-transform": "translateX("+_this.config.menu_width+"px"+")",
"-ms-transform": "translateX("+_this.config.menu_width+"px"+")",
"transform": "translateX("+_this.config.menu_width+"px"+")"
});
overlay.css("visibility", "visible").fadeTo("slow",0.5);
}
});
}
};
MobileMenu.defaults = MobileMenu.prototype.defaults;
$.fn.mobile_menu = function(options) {
return this.each(function() {
new MobileMenu(this, options).init();
});
};
mobileApp.MobileMenu = MobileMenu;
})(mobileApp,jQuery);