Don’t do the hiding and showing of the menu and toggle button via js but use css media queries instead as that is what they were designed for.
All the js needs to do is toggle the menu on and off when the toggle button is clicked. If you hide the toggle button at larger screen sizes with media queries then it won’t get opened by accident. Also add css classes with js to open the menu rather than directly using the style attribute.
Your example would not work if someone resizes their window unless they also clicked refresh and then would not work when the window was opened wider again.
Do not use JavaScript at all, is the answer to your query.
When it comes to things like adjusting the presentation based on the screen width, CSS media queries are the much better and more reliable technique to use instead.
Is that something that can be experienced and troubleshooted on the jsfiddle example? If not, we’ll need to see some demo code from you that demonstrates the problem.
@James_Hibbard this code based on your example above seems to work fine besides one small issue — if you resize to desktop, then back to mobile, you need to double click the toggler:
CSS:
#menu-mainmenu {display: none}
#menu-mainmenu ul {display: block; padding: 0 1em 0 1em}
#menu-mainmenu li {display: block; margin: 1em auto 0; list-style-type: none}
#menu-mainmenu a {display: block; text-align: center; font-family: tahoma; color: #000}
#menu-mainmenu a:hover, .current-menu-item {color: #d3b258}
@media screen and (min-width: 769px) {
#menu-mainmenu {display: block}
#menu-mainmenu, #menu-mainmenu ul {text-align: center}
#menu-mainmenu li {display: inline-block; padding: 1em 0 1em 2em}
#menu-mainmenu a {display: inline-block; max-width: initial; padding: 1em 0 1em 2em; margin: -1em 0 -1em -2em}
}
Borrowing Paul’s js then I would hide the nav menu off to the side (no display none) and then when the class is added you can slide it into view with a transition. You can’t animate display:none/block.
This is very rough but I usually do something like this:
I had a play around with the demo and have a couple more questions. I’ll see what I can find on Google, then start a new thread if I can’t find what I’m looking for.
Can you post some HTML to go with the CSS / JavaScript. It doesn’t have to be a link to your site (or even your real code), rather just a snippet that makes it possible to recreate what you are working with.