This is the first JS/JQuery script I had written for my website, few months ago. It toggles/hide/show menus (and some sub-menus/interactions) when a user clicks on specific items from the navbar.
Right now I have 6 main menus, and I believe my code is far from being optimized.
$(document).ready(function(){
$(document).click(function(event) {
$("#website-header #tab-lang").hide("slide", { direction: "right" }, 300);
$("#website-header #tab-apps").hide("slide", { direction: "right" }, 300);
$("#website-header #tab-apps #apps-reveal").delay(300).hide(0);
$("#website-header #tab-dashboard").hide("slide", { direction: "right" }, 300);
$("#website-header #tab-login").hide("slide", { direction: "right" }, 300);
$("#website-footer #tab-about").hide("slide", { direction: "down" }, 300);
$("#website-footer #tab-about #credits-reveal").delay(300).hide(0);
$("#website-footer #tab-contact").hide("slide", { direction: "down" }, 300);
});
// lang tab
$("#website-header #tab-lang").hide().click(function(event) {
event.stopPropagation();
});
$("#website-header #nav-lang").click(function(event) {
event.stopPropagation();
$("#website-header #tab-lang").toggle("slide", { direction: "right" }, 300);
$("#website-header #tab-apps").hide("slide", { direction: "right" }, 300);
$("#website-header #tab-apps #apps-reveal").delay(300).hide(0);
$("#website-header #tab-dashboard").hide("slide", { direction: "right" }, 300);
$("#website-header #tab-login").hide("slide", { direction: "right" }, 300);
$("#website-footer #tab-about").hide("slide", { direction: "down" }, 300);
$("#website-footer #tab-about #credits-reveal").delay(300).hide(0);
$("#website-footer #tab-contact").hide("slide", { direction: "down" }, 300);
});
// apps tab
$("#website-header #tab-apps").hide().click(function(event) {
event.stopPropagation();
});
$("#website-header #nav-apps").click(function(event) {
event.stopPropagation();
$("#website-header #tab-lang").hide("slide", { direction: "right" }, 300);
$("#website-header #tab-apps").toggle("slide", { direction: "right" }, 300);
$("#website-header #tab-apps #apps-reveal").delay(300).hide(0);
$("#website-header #tab-dashboard").hide("slide", { direction: "right" }, 300);
$("#website-header #tab-login").hide("slide", { direction: "right" }, 300);
$("#website-footer #tab-about").hide("slide", { direction: "down" }, 300);
$("#website-footer #tab-about #credits-reveal").delay(300).hide(0);
$("#website-footer #tab-contact").hide("slide", { direction: "down" }, 300);
});
$("#website-header #apps-toggle").click( function() {
$("#website-header #tab-apps #apps-reveal").slideToggle(300);
} );
// dashboard tab
$("#website-header #tab-dashboard").hide().click(function(event) {
event.stopPropagation();
});
$("#website-header #nav-dashboard").click(function(event) {
event.stopPropagation();
$("#website-header #tab-lang").hide("slide", { direction: "right" }, 300);
$("#website-header #tab-apps").hide("slide", { direction: "right" }, 300);
$("#website-header #tab-apps #apps-reveal").delay(300).hide(0);
$("#website-header #tab-dashboard").toggle("slide", { direction: "right" }, 300);
$("#website-header #tab-login").hide("slide", { direction: "right" }, 300);
$("#website-footer #tab-about").hide("slide", { direction: "down" }, 300);
$("#website-footer #tab-about #credits-reveal").delay(300).hide(0);
$("#website-footer #tab-contact").hide("slide", { direction: "down" }, 300);
});
// login tab
$("#website-header #tab-login").hide().click(function(event) {
event.stopPropagation();
});
$("#website-header #nav-login").click(function(event) {
event.stopPropagation();
$("#website-header #tab-lang").hide("slide", { direction: "right" }, 300);
$("#website-header #tab-apps").hide("slide", { direction: "right" }, 300);
$("#website-header #tab-apps #apps-reveal").delay(300).hide(0);
$("#website-header #tab-dashboard").hide("slide", { direction: "right" }, 300);
$("#website-header #tab-login").toggle("slide", { direction: "right" }, 300);
$("#website-footer #tab-about").hide("slide", { direction: "down" }, 300);
$("#website-footer #tab-about #credits-reveal").delay(300).hide(0);
$("#website-footer #tab-contact").hide("slide", { direction: "down" }, 300);
});
// about tab
$("#website-footer #tab-about").hide().click(function(event) {
event.stopPropagation();
});
$("#website-footer #nav-about").click(function(event) {
event.stopPropagation();
$("#website-header #tab-lang").hide("slide", { direction: "right" }, 300);
$("#website-header #tab-apps").hide("slide", { direction: "right" }, 300);
$("#website-header #tab-apps #apps-reveal").delay(300).hide(0);
$("#website-header #tab-dashboard").hide("slide", { direction: "right" }, 300);
$("#website-header #tab-login").hide("slide", { direction: "right" }, 300);
$("#website-footer #tab-about").toggle("slide", { direction: "down" }, 300);
$("#website-footer #tab-about #credits-reveal").delay(300).hide(0);
$("#website-footer #tab-contact").hide("slide", { direction: "down" }, 300);
});
$("#website-footer #credits-toggle").click( function() {
$("#website-footer #tab-about #credits-reveal").slideToggle(300);
} );
// contact tab
$("#website-footer #tab-contact").hide().click(function(event) {
event.stopPropagation();
});
$("#website-footer #nav-contact").click(function(event) {
event.stopPropagation();
$("#website-header #tab-lang").hide("slide", { direction: "right" }, 300);
$("#website-header #tab-apps").hide("slide", { direction: "right" }, 300);
$("#website-header #tab-apps #apps-reveal").delay(300).hide(0);
$("#website-header #tab-dashboard").hide("slide", { direction: "right" }, 300);
$("#website-header #tab-login").hide("slide", { direction: "right" }, 300);
$("#website-footer #tab-about").hide("slide", { direction: "down" }, 300);
$("#website-footer #tab-about #credits-reveal").delay(300).hide(0);
$("#website-footer #tab-contact").toggle("slide", { direction: "down" }, 300);
$("#website-footer #tab-contact #contact-callback").hide().css('visibility','visible');
$("#website-footer #tab-contact #callback-message").hide().css('visibility','visible');
$("#website-footer #tab-contact #callback-dismiss").hide().css('visibility','visible');
});
})
Basically I'm repeating the same thing, when a user clicks on a navbar item, I close every other menus and open the one the user requested (and reset the sub-menus if there were any). When the user clicks on the documents (or anywhere that is outside the menu they opened) I also close everything.
I'm afraid my code, even though it works fine, might be a bit repetitive and somewhat ressources' greedy (does the $(document).click(function(event)
trigger even when there is nothing to close?).
I'm looking for some recommendations, ideas, or models from more experienced devs than I am.