html - Responsive Design - Toggle a Div's Visibility - how to integrate javascript with css @media -
i started previous thread same goal have real question , have created 2 fiddles jsfiddle #1 , jsfiddles jsfiddle #2 show problem.
i attempting use principles of responsive design adapt changing screen size. has these 2 parts
use css hide/show vertical menu div (
main-nav-vert
) using@media
based on browser width.use menu button hide/show same div
main-nav-vert
using javascript.
the problem having can find way integrate javascript @media
rules. open way (but without jquery)
the problem: test these fiddles , see problem: reduce window size see impact of @media
, should hide menu on left. expand window , menu should reappear. shrink window again until menu disappears. click button , menu appears. click button second time hide it. now, expanding window no longer show menu.
try #1 (jsfiddle #1)
- javascript changes same
display
property ofmain-nav-vert
@media
changing. - javascript changes element not underlying css (as shown in inspector).
- the element values set javascript take precedence on css property set
@media
, ... - once button pressed,
@media
settings no longer work - being overridden.
try #2 (jsfiddle #2)
- javascript alternately sets new class
main-nav-vert
div (hidden
,visible
) each click of button. - the
display
propertymain-nav-vert
set@media
take precedence ondisplay
propertyhidden
class , button not hide menu ... unless - i override
display
property using!important
invisible
,hidden
classes,@media
won't work before.
please help. there must better way. looking pure js answer, please no jquery. thanks.
is need? having hard time understanding want links here 2 different solutions.
css
.main-content { max-width:808px; width: auto; padding:0 9px 0 9px; } .main-nav-vert { display: block; float:left; width:145px; border:solid 2px #00f; } .visible{ display: block !important; } .hidden{ display: none !important; } @media (min-width: 450px) { #main-nav-vert{ display: block; } } @media (max-width: 449px){ #main-nav-vert{ display: none; } .hidden{ display: none; } }
css
.main-content { max-width:808px; width: auto; padding:0 9px 0 9px; } .main-nav-vert { display: block; float:left; width:145px; border:solid 2px #00f; } .visible{ display: block !important; } .hidden{ display: none !important; } @media (min-width: 450px) { #main-nav-vert{ display: block; } } @media (max-width: 449px){ #main-nav-vert{ display: none !important; } } #main-nav-vert{ display: none; }
Comments
Post a Comment