2

I want to keep the mobile menu open when the website is loaded for the first time magento2. After that, it should work as the default on open close by click. Can anyone suggest to me?

enter image description here

2
  • Do you mean that sleeping & livings, brands should be open for first time.? Sep 8, 2020 at 7:39
  • Yes, These should be expandable on first load. Sep 8, 2020 at 7:43

1 Answer 1

1

You can do this using jQuery, try the below code in the toggle function of the menu.js file. Override this file to your theme or you can extend it also.

File: lib/web/mage/menu.js

Code to add in function:

$('.level0.parent > a').each(function(){
    if (!$(this).hasClass('_ignoreTrigger')) {
        $(this).addClass('ui-state-active');
        $(this).next().show();
        $(this).addClass('_ignoreTrigger');
    }
});

Toggle function will look like this:

toggle: function () {
    var html = $('html');

    if (html.hasClass('nav-open')) {
        html.removeClass('nav-open');
        setTimeout(function () {
            html.removeClass('nav-before-open');
        }, this.options.hideDelay);
    } else {
        html.addClass('nav-before-open');
        setTimeout(function () {
            html.addClass('nav-open');
            /** Auto open submenus for first time **/
            $('.level0.parent > a').each(function(){
                if (!$(this).hasClass('_ignoreTrigger')) {
                    $(this).addClass('ui-state-active');
                    $(this).next().show();
                    $(this).addClass('_ignoreTrigger');
                }
            });
            /** END **/
        }, this.options.showDelay);
    }
},
4
  • It opens the first time but when I click on the same link then again open it and not working properly. Sep 8, 2020 at 10:05
  • Yes, it will open after each page load. but if you dont want to open it on each page load then you have to use cookies. So instead of addClass _ignoreTrigger, you can set cookie there and then check the cookie and if cookie not set then show submenus. Sep 8, 2020 at 13:06
  • Yes, maybe it will work. I will try this and let you know if work. Sep 8, 2020 at 13:45
  • Yeah sure. If my answer is helpful to you then mark it as accepted and upvote. Thank you, Sep 9, 2020 at 3:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.