I’m working on a one-page WordPress website and the page scrolls to each section when a menu item is clicked. It works well on desktops/laptops but on mobile devices when the hamburger menu is tapped, the menu opens and it blocks the screen. I would like to hide the dropdown menu list/section only on mobile. At the moment I have to close the menu by tapping on "X" close button.
I have searched for a solution and I found some JS code here in Stackoverflow but it has not worked.
Here is the JS code:
<script>
function closeFunction()
{
document.getElementById('bs-example-navbar-collapse-1').style.display='none';
}
</script>
I’m not so experienced in JS but with some guidance I fix this, please help.
2
Answers
If you want to hide menu then you just need to hide hamburger icon (because it will not have functionality at all), right? If there is no hamburger to click, dropdown menu will not appear. You just need to use CSS: define min width when the hamburger is visible. You should consider mobile view can be landscape and portrait (widths are different). It is possible to handle screen orientation too, but then you need Javascript.
Also, there is a way to detect if device is mobile device, but I would not recommend doing that. Detecting a mobile browser
Edited
You should define onclick event that triggers your function
You can simply make an event listener, and hide it.