Skip to content

Instantly share code, notes, and snippets.

@culttm
Last active October 7, 2018 18:06
Show Gist options
  • Save culttm/d75aed380404d5c0111ee674ef75ac53 to your computer and use it in GitHub Desktop.
Save culttm/d75aed380404d5c0111ee674ef75ac53 to your computer and use it in GitHub Desktop.
Example for mobile menu
$(document).ready(function () {
var MenuApp = (function () {
var Menu = function (options) {
this.el = options.el;
this.trigger = options.trigger;
};
Menu.prototype.init = function () {
this.addEventListeners()
};
Menu.prototype.addEventListeners = function () {
$(this.trigger).on('click.menu', $.proxy(this.toggle, this));
$(document).on('click.menu', $.proxy(this.hideByDocument, this));
};
Menu.prototype.toggle = function () {
$(this.el).toggle();
};
Menu.prototype.show = function () {
$(this.el).show();
};
Menu.prototype.hide = function () {
$(this.el).hide();
};
Menu.prototype.checkElement = function (target, el) {
return !$(el).is(target) && $(el).has(target).length === 0
};
Menu.prototype.hideByDocument = function (e) {
if(this.checkElement(e.target, this.trigger) && this.checkElement(e.target, this.el)) {
this.hide();
}
};
return Menu;
})();
var menuApp = new MenuApp({
el: '#menu',
trigger: '#menu-btn'
});
menuApp.init();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment