Skip to content

Instantly share code, notes, and snippets.

@kodie
Created September 21, 2021 17:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kodie/9fd0ab72d3281f17342123636bfedc57 to your computer and use it in GitHub Desktop.
Save kodie/9fd0ab72d3281f17342123636bfedc57 to your computer and use it in GitHub Desktop.
A simple mobile navigation jQuery plugin - Originally created by the good people over at PSD2HTML.com - Simply here so we can pull the code in remotely
/*
* Simple Mobile Navigation
*/
;(function($) {
function MobileNav(options) {
this.options = $.extend({
container: null,
hideOnClickOutside: false,
menuActiveClass: 'nav-active',
menuOpener: '.nav-opener',
menuDrop: '.nav-drop',
toggleEvent: 'click',
outsideClickEvent: 'click touchstart pointerdown MSPointerDown'
}, options);
this.initStructure();
this.attachEvents();
}
MobileNav.prototype = {
initStructure: function() {
this.page = $('html');
this.container = $(this.options.container);
this.opener = this.container.find(this.options.menuOpener);
this.drop = this.container.find(this.options.menuDrop);
},
attachEvents: function() {
var self = this;
if(activateResizeHandler) {
activateResizeHandler();
activateResizeHandler = null;
}
this.outsideClickHandler = function(e) {
if(self.isOpened()) {
var target = $(e.target);
if(!target.closest(self.opener).length && !target.closest(self.drop).length) {
self.hide();
}
}
};
this.openerClickHandler = function(e) {
e.preventDefault();
self.toggle();
};
this.opener.on(this.options.toggleEvent, this.openerClickHandler);
},
isOpened: function() {
return this.container.hasClass(this.options.menuActiveClass);
},
show: function() {
this.container.addClass(this.options.menuActiveClass);
if(this.options.hideOnClickOutside) {
this.page.on(this.options.outsideClickEvent, this.outsideClickHandler);
}
},
hide: function() {
this.container.removeClass(this.options.menuActiveClass);
if(this.options.hideOnClickOutside) {
this.page.off(this.options.outsideClickEvent, this.outsideClickHandler);
}
},
toggle: function() {
if(this.isOpened()) {
this.hide();
} else {
this.show();
}
},
destroy: function() {
this.container.removeClass(this.options.menuActiveClass);
this.opener.off(this.options.toggleEvent, this.clickHandler);
this.page.off(this.options.outsideClickEvent, this.outsideClickHandler);
}
};
var activateResizeHandler = function() {
var win = $(window),
doc = $('html'),
resizeClass = 'resize-active',
flag, timer;
var removeClassHandler = function() {
flag = false;
doc.removeClass(resizeClass);
};
var resizeHandler = function() {
if(!flag) {
flag = true;
doc.addClass(resizeClass);
}
clearTimeout(timer);
timer = setTimeout(removeClassHandler, 500);
};
win.on('resize orientationchange', resizeHandler);
};
$.fn.mobileNav = function(opt) {
var args = Array.prototype.slice.call(arguments);
var method = args[0];
return this.each(function() {
var $container = jQuery(this);
var instance = $container.data('MobileNav');
if (typeof opt === 'object' || typeof opt === 'undefined') {
$container.data('MobileNav', new MobileNav($.extend({
container: this
}, opt)));
} else if (typeof method === 'string' && instance) {
if (typeof instance[method] === 'function') {
args.shift();
instance[method].apply(instance, args);
}
}
});
};
}(jQuery));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment