-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathadaptivemenu.jquery.js
More file actions
69 lines (60 loc) · 2.17 KB
/
adaptivemenu.jquery.js
File metadata and controls
69 lines (60 loc) · 2.17 KB
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
(function($) {
//default values
var config = {
'width': 481, //property defines maximum screen width at which menu will be replaced with a link
'text': 'Show menu', //link anchor
'openMenuLinkClass': 'adaptiveMenuTrigger' //link CSS class
};
var methods = {
'init': function(settings) {
//using user defined values
if (settings) $.extend(config, settings);
return this.each($.proxy(function(index, menu) {
if (isSmallScreen(config)) {
hideMenu(menu, config);
}
}, this));
},
'show': function() {
var link = this.parent().find('a.' + config.openMenuLinkClass);
link.trigger('click.adaptiveMenu');
},
'hide': function() {
hideMenu(this[0]);
}
};
$.fn.adaptiveMenu = function(method) {
if (methods[method]) {
return methods[method].apply( this, Array.prototype.slice.call(arguments, 1));
} else if (typeof method === 'object' || !method) {
return methods.init.apply(this, arguments);
} else {
$.error('Method ' + method + ' does not exist on jQuery.adaptiveMenu');
}
};
// private functions definition
function isSmallScreen() {
var viewport = $(window).width();
if (viewport < config.width) {
return true;
}
return false;
}
function showMenu(event) {
event.preventDefault();
$(event.data.menu).removeClass('hidden');
$(event.currentTarget).addClass('hidden');
}
function hideMenu(menu) {
$(menu).addClass('hidden');
var link = $(menu).parent().find('a.' + config.openMenuLinkClass);
if (link.length === 0) {
link = $('<a>').attr('class', config.openMenuLinkClass).attr('href', '#').html(config.text);
link.insertAfter($(menu));
link.bind('click.adaptiveMenu', {'menu': menu}, showMenu);
}
else {
link.removeClass('hidden');
}
}
})(jQuery);