1 |
cc7b6b59
|
Julien Enselme
|
(function($){
|
2 |
|
|
|
3 |
|
|
|
4 |
|
|
var menuCount = 0;
|
5 |
|
|
|
6 |
|
|
|
7 |
|
|
$.fn.mobileMenu = function(options){
|
8 |
|
|
|
9 |
|
|
|
10 |
|
|
var settings = {
|
11 |
|
|
switchWidth: 750,
|
12 |
|
|
topOptionText: 'Select menu item',
|
13 |
|
|
indentString: ' '
|
14 |
|
|
};
|
15 |
|
|
|
16 |
|
|
|
17 |
|
|
|
18 |
|
|
function isList($this){
|
19 |
|
|
return $this.is('ul, ol');
|
20 |
|
|
}
|
21 |
|
|
|
22 |
|
|
|
23 |
|
|
|
24 |
|
|
function isMobile(){
|
25 |
|
|
return ($(window).width() < settings.switchWidth);
|
26 |
|
|
}
|
27 |
|
|
|
28 |
|
|
|
29 |
|
|
|
30 |
|
|
function menuExists($this){
|
31 |
|
|
|
32 |
|
|
|
33 |
|
|
if($this.attr('id')){
|
34 |
|
|
return ($('#mobileMenu_'+$this.attr('id')).length > 0);
|
35 |
|
|
}
|
36 |
|
|
|
37 |
|
|
|
38 |
|
|
else {
|
39 |
|
|
menuCount++;
|
40 |
|
|
$this.attr('id', 'mm'+menuCount);
|
41 |
|
|
return ($('#mobileMenu_mm'+menuCount).length > 0);
|
42 |
|
|
}
|
43 |
|
|
}
|
44 |
|
|
|
45 |
|
|
|
46 |
|
|
|
47 |
|
|
function goToPage($this){
|
48 |
|
|
if($this.val() !== null){document.location.href = $this.val()}
|
49 |
|
|
}
|
50 |
|
|
|
51 |
|
|
|
52 |
|
|
|
53 |
|
|
function showMenu($this){
|
54 |
|
|
$this.css('display', 'none');
|
55 |
|
|
$('#mobileMenu_'+$this.attr('id')).show();
|
56 |
|
|
}
|
57 |
|
|
|
58 |
|
|
|
59 |
|
|
|
60 |
|
|
function hideMenu($this){
|
61 |
|
|
$this.css('display', '');
|
62 |
|
|
$('#mobileMenu_'+$this.attr('id')).hide();
|
63 |
|
|
}
|
64 |
|
|
|
65 |
|
|
|
66 |
|
|
|
67 |
|
|
function createMenu($this){
|
68 |
|
|
if(isList($this)){
|
69 |
|
|
|
70 |
|
|
|
71 |
|
|
var selectString = '<select id="mobileMenu_'+$this.attr('id')+'" class="mobileMenu">';
|
72 |
|
|
|
73 |
|
|
|
74 |
|
|
selectString += '<option value="">'+settings.topOptionText+'</option>';
|
75 |
|
|
|
76 |
|
|
|
77 |
|
|
$this.find('li').each(function(){
|
78 |
|
|
|
79 |
|
|
|
80 |
|
|
var levelStr = '';
|
81 |
|
|
var len = $(this).parents('ul, ol').length;
|
82 |
|
|
for(i=1;i<len;i++){levelStr += settings.indentString;}
|
83 |
|
|
|
84 |
|
|
|
85 |
|
|
var link = $(this).find('a:first-child').attr('href');
|
86 |
|
|
var text = levelStr + $(this).clone().children('ul, ol').remove().end().text();
|
87 |
|
|
|
88 |
|
|
|
89 |
|
|
selectString += '<option value="'+link+'">'+text+'</option>';
|
90 |
|
|
});
|
91 |
|
|
|
92 |
|
|
selectString += '</select>';
|
93 |
|
|
|
94 |
|
|
|
95 |
|
|
$this.parent().append(selectString);
|
96 |
|
|
|
97 |
|
|
|
98 |
|
|
$('#mobileMenu_'+$this.attr('id')).change(function(){
|
99 |
|
|
goToPage($(this));
|
100 |
|
|
});
|
101 |
|
|
|
102 |
|
|
|
103 |
|
|
showMenu($this);
|
104 |
|
|
} else {
|
105 |
|
|
alert('mobileMenu will only work with UL or OL elements!');
|
106 |
|
|
}
|
107 |
|
|
}
|
108 |
|
|
|
109 |
|
|
|
110 |
|
|
|
111 |
|
|
function run($this){
|
112 |
|
|
|
113 |
|
|
|
114 |
|
|
if(isMobile() && !menuExists($this)){
|
115 |
|
|
createMenu($this);
|
116 |
|
|
}
|
117 |
|
|
|
118 |
|
|
|
119 |
|
|
else if(isMobile() && menuExists($this)){
|
120 |
|
|
showMenu($this);
|
121 |
|
|
}
|
122 |
|
|
|
123 |
|
|
|
124 |
|
|
else if(!isMobile() && menuExists($this)){
|
125 |
|
|
hideMenu($this);
|
126 |
|
|
}
|
127 |
|
|
|
128 |
|
|
}
|
129 |
|
|
|
130 |
|
|
|
131 |
|
|
|
132 |
|
|
return this.each(function() {
|
133 |
|
|
|
134 |
|
|
|
135 |
|
|
if(options){$.extend(settings, options);}
|
136 |
|
|
|
137 |
|
|
|
138 |
|
|
var $this = $(this);
|
139 |
|
|
|
140 |
|
|
|
141 |
|
|
$(window).resize(function(){run($this);});
|
142 |
|
|
|
143 |
|
|
|
144 |
|
|
run($this);
|
145 |
|
|
|
146 |
|
|
});
|
147 |
|
|
|
148 |
|
|
};
|
149 |
|
|
|
150 |
|
|
})(jQuery); |