How to close a div when navigating to new page

So I have created a custom menu button which displays a div.

The div is opened via an ‘on click’ interaction.

The div contains various text elements which navigate to other pages on the site.

If I follow a link to a new page and the go back the div is still in its open state.

How to I close the div when navigating away from the page?

Is there a neat JavaScript or custom code you could recommend?

Thanks

Does this WF University video help? If not, it would be helpful to share a link to your project. Thanks.