In Divi WordPress theme how can I create mobile menu with the option of closing and opening the sub menus?
Here is what I mean:
CLOSED STATE OF THE SUB-MENUS:
and OPENED STATE OF THE SUB-MENUS:
Thanks!
Here is how to do it in 3 steps:
STEP 1:
Create new file menu.js and put it in your Divi's Child Theme (in my case my Child theme is called derma):
Put the following code in the menu.js:
jQuery(document).ready(function($){ function ds_setup_collapsible_submenus() { var $menu = $('.et_mobile_menu'), top_level_link = '.et_mobile_menu .menu-item-has-children > a'; $menu.find('a').each(function() { $(this).off('click'); if ( $(this).is(top_level_link) ) { $(this).attr('href', '#'); } if ( ! $(this).siblings('.sub-menu').length ) { $(this).on('click', function(event) { $(this).parents('.mobile_nav').trigger('click'); }); } else { $(this).on('click', function(event) { event.preventDefault(); $(this).parent().toggleClass('visible'); }); } }); } setTimeout(function() { ds_setup_collapsible_submenus(); }, 700); });
STEP 2:
Link the menu.js in the integration tab in Divi >> Theme Options >> Integration >> Add code to the < head > of your blog.
In my case the code (the full path to the menu.js) is:
<script type='text/javascript' src='https://derma-am.com/wp-content/themes/derma/menu.js'></script>
STEP 3:
Add the following code in your Divi's style.css:
/* Closing sub menu - mobile version */ .et_pb_menu .et_mobile_menu .menu-item-has-children > a, #main-header .et_mobile_menu .menu-item-has-children > a { background-color: transparent; position: relative; } .et_pb_menu .et_mobile_menu .menu-item-has-children > a:after, #main-header .et_mobile_menu .menu-item-has-children > a:after { font-family: 'ETmodules', sans-serif; text-align: center; speak: none; font-weight: 500; font-variant: normal; text-transform: none; -webkit-font-smoothing: antialiased; position: absolute; font-size: 14px; font-style: italic; content: '\4c'; top: 11px; right: 13px; } .et_pb_menu .et_mobile_menu .menu-item-has-children.visible > a:after, #main-header .et_mobile_menu .menu-item-has-children.visible > a:after { content: '\4d'; } .et_pb_menu .et_mobile_menu ul.sub-menu, #main-header .et_mobile_menu ul.sub-menu, .et-db #et-boc .et-l .et_pb_menu .et_mobile_menu li ul.sub-menu { display: none !important; visibility: hidden !important; transition: all 1.5s ease-in-out; } .et_pb_menu .et_mobile_menu li.visible > ul.sub-menu, #main-header .et_mobile_menu li.visible > ul.sub-menu, .et-db #et-boc .et-l #main-header .et_mobile_menu li.visible > ul.sub-menu, .et-db #et-boc .et-l .et_pb_fullwidth_menu .et_mobile_menu li.visible > ul.sub-menu, .et-db #et-boc .et-l .et_pb_menu .et_mobile_menu li.visible > ul.sub-menu { display: block !important; visibility: visible !important; }
You can add any text on: line 19 (for example): content: 'open \4c'; and on line 26 (for example): content: 'close \4d';
Voila!
Enjoy 🙂
Cheers