I have a web application with a left-hand side menu. The menu is useful when the browser window is wide (e.g., desktops), but takes too much space when the window is narrow (e.g., mobile).
Using css @media
stuff, I can make it so that if you make the browser window smaller than some resize width, it will switch to mobile mode. In this mode, I still want the menu to be available, but for it to be hidden until the user clicks a button to bring it up.
This is easy enough, but I am running into a problem. If the normal desktop user:
- Switches to Mobile Mode (resizes the browser smaller than the resize width)
- Turns the menu on (javascript to set its
display:
toblock
) - Turns the menu off (js to set its
display:
tonone
) - Switches back to Desktop Mode (resizes the browser to be wide again)
Then the menu is still using display:none
and is invisible, even though it should always be available in desktop mode.
Here is a working illustration of the problem.
My question: is there something I can do to prevent this? Perhaps a way to detect when the browser is switching the stylesheet it is using, and to reset the menu bar to display:block
when desktop mode is re-entered?
Basic HTML code:
<link rel="stylesheet" href="desktop.css" media="all and (min-width: 600px)"/>
<link rel="stylesheet" href="mobile.css" media="all and (max-width: 600px)"/>
...
<div id="left"> ... menu bar ... </div>
<div id="right"> ... content ... </div>
Mobile stylesheet:
#left { display: none; position: fixed; z-index: 100; ... }
#right { left: 0px; ... }
Desktop stylesheet
#left { display: block; position: fixed; left: 0; ...}
#right { position: fixed; left: 170px; ...}
Javascript for toggling the menu
function toggleLeft() { $("#left").toggle(); } // Using jQuery
Thanks very much!!