forked from arifwidip/mobilemenu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.mobilemenu.js
79 lines (66 loc) · 1.79 KB
/
jquery.mobilemenu.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 Mobile Menu
* Turn unordered list menu into dropdown select menu
* version 1.1(27-JULY-2013)
*
* Built on top of the jQuery library
* http://jquery.com
*
* Documentation
* http://github.com/mambows/mobilemenu
*/
(function($){
$.fn.mobileMenu = function(options) {
var defaults = {
defaultText: 'Navigate to...',
className: 'select-menu',
subMenuClass: 'sub-menu',
subMenuDash: '–',
linkAnchor: ''
},
settings = $.extend( defaults, options ),
el = $(this);
this.each(function(){
var $el = $(this),
$select_menu;
// ad class to submenu list
$el.find('ul').addClass(settings.subMenuClass);
// Create base menu
var $select_menu = $('<select />',{
'class' : settings.className + ' ' + el.get(0).className
}).insertAfter( $el );
// Create default option
$('<option />', {
"value" : '#',
"text" : settings.defaultText
}).appendTo( $select_menu );
// Create select option from menu
$el.find('a').each(function(){
var $this = $(this),
optText = ' ' + $this.text(),
optSub = $this.parents( '.' + settings.subMenuClass ),
len = optSub.length,
dash;
// if menu has sub menu
if( $this.parents('ul').hasClass( settings.subMenuClass ) ) {
dash = Array( len+1 ).join( settings.subMenuDash );
optText = dash + optText;
}
// Now build menu and append it
$('<option />', {
"value" : this.href + settings.linkAnchor,
"html" : optText,
"selected" : (this.href == window.location.href)
}).appendTo( $select_menu );
}); // End el.find('a').each
// Change event on select element
$select_menu.change(function(){
var locations = $(this).val();
if( locations !== '#' ) {
window.location.href = $(this).val();
};
});
}); // End this.each
return this;
};
})(jQuery);