Jump to content

Mobile Menu on Desktop Click Away to Close

Recommended Posts

  • Replies 4
  • Views 204
  • Created
  • Last Reply

Top Posters In This Topic

On 9/3/2024 at 3:09 PM, BloomStudio said:

Sorry, no I mean click anywhere on the website and the menu would close automatically. Is this possible?

Try this code to Website Tools > Code Injection > Footer

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.0/jquery.min.js"></script>
<script>
  $(document).ready(() => {
  document.querySelector('.header-menu').id = 'test';
  document.querySelector('.header-display-mobile .header-burger-btn').id = 'test-btn';
  
  function handleMenu(e) {
    const els = e.path;
    const menu = els.find(el => el.id === 'test')
    const btn = els.find(el => el.id === 'test-btn')
    if (!menu && !btn) {
      setTimeout(() => {
        const isOpen = document.querySelector('.js-header-burger-close-title').getAttribute('hidden') === null;
  
    if (isOpen) document.querySelector('.header-display-mobile .header-burger-btn').click();
      }, 100)
    }
    
  }
  window.addEventListener('click', handleMenu);

})
</script>

 

Email me if you have need any help (free, of course.). Answer within 24 hours. 
Or send to forum message

Contact Customer Care - Learn CSS - Buy me a coffee (thank you!)

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

×
×
  • Create New...

Squarespace Webinars

Free online sessions where you’ll learn the basics and refine your Squarespace skills.

Hire a Designer

Stand out online with the help of an experienced designer or developer.