The final JS to get our menu working as best as we can is below. There are still two or three oddities that I am pretty sure are caused by poor/faulty coding with Hubspots default menu code. Hopefully they can fix it. One note about this solution is during times of heavy load/slow speeds you can see a slide down, up, down if you look really closely. In testing only one of my testers noticed (and he’s the pickiest person I know! lol)
$(function() {
/**
* Mobile Nav
*
* Hubspot Standard Toggle Menu
*/
$('.custom-menu-primary').addClass('js-enabled');
/* Mobile button with three lines icon */
$('.custom-menu-primary .hs-menu-wrapper').before('<div class="mobile-trigger"><i></i></div>');
/* Uncomment for mobile button that says 'MENU'
$('.custom-menu-primary .hs-menu-wrapper').before('<div class="mobile-trigger">MENU</div>');
*/
$('.custom-menu-primary .flyouts .hs-item-has-children > a').after('<div class="child-trigger"><i></i></div>');
$('.mobile-trigger').click(function() {
$(this).next('.custom-menu-primary .hs-menu-wrapper').slideToggle(250);
$('body').toggleClass('mobile-open');
$('.child-trigger').removeClass('child-open');
$('.hs-menu-children-wrapper').slideUp(250);
return false;
});
$('.child-trigger').click(function() {
expandMenu(this);
});
$('.custom-menu-primary .flyouts .hs-item-has-children > a').click(function(event) {
var t = document.querySelector(".hs-menu-wrapper.flyouts > ul > li > ul.open-menu-list"), r = this.parentNode.querySelector("ul");
if (t) {
t.removeAttribute("style");
t.classList.remove("open-menu-list")
}
t && t.parentNode.querySelector("a").setAttribute("aria-expanded", "false");
expandMenu($(this).next('.child-trigger'));
return false;
});
function expandMenu(childTriggerElement) {
$(childTriggerElement).parent().siblings('.hs-item-has-children').find('.child-trigger').removeClass('child-open');
$(childTriggerElement).parent().siblings('.hs-item-has-children').find('.hs-menu-children-wrapper').slideUp(250);
$(childTriggerElement).next('.hs-menu-children-wrapper').slideToggle(250);
$(childTriggerElement).next('.hs-menu-children-wrapper').children('.hs-item-has-children').find('.hs-menu-children-wrapper').slideUp(250);
$(childTriggerElement).next('.hs-menu-children-wrapper').children('.hs-item-has-children').find('.child-trigger').removeClass('child-open');
$(childTriggerElement).toggleClass('child-open');
return false;
}
});