I started a previous thread with the same goal but now I have the real question and have created two fiddles jsfiddle #1 AND jsfiddles jsfiddle #2 to show the problem.
I am attempting to use principles of responsive design to adapt to changing screen size. It has these two parts
-
Use CSS to hide/show a vertical menu div (
main-nav-vert) using@mediabased on browser width. -
Use a Menu button to also hide/show the same div
main-nav-vertusing javascript.
The problem I am having is that I can find a way to integrate the javascript with the @media rules. I am open to another way (but without jQuery)
The Problem: To test these fiddles and see the problem: Reduce the window size to see the impact of @media and it should hide the menu on the left. Expand the window and the menu should reappear. Now shrink the window again until the menu disappears. Click the button and the menu appears. Click the button a second time to hide it. NOW, expanding the window will no longer show the menu.
Try #1 (jsfiddle #1)
- javascript changes the same
displayproperty ofmain-nav-vertthat@mediais changing. - javascript only changes the element NOT the underlying CSS (as shown in the inspector).
- The element values set by javascript will take precedence over the CSS property set by
@mediaand so ... - once the button is pressed, the
@mediasettings will no longer work - being overridden.
Try #2 (jsfiddle #2)
- javascript alternately sets a new class to the
main-nav-vertdiv (hiddenandvisible) with each click of the button. - the
displayproperty formain-nav-vertset by@mediawill take precedence over thedisplayproperty from thehiddenclass and the button will not hide the menu ... UNLESS - I override the
displayproperty by using!importantin thevisibleandhiddenclasses, but then@mediawon't work as before.
Please help. There must be a better way. I am looking for a pure js answer, please no jQuery. Thanks.
Aucun commentaire:
Enregistrer un commentaire