I'm trying to make a sidebar menu to achieve a responsive design.
I have a container div #wrapper for the page and a sidebar div for the menu #sidebar-wrapper.
On desktop view when screen width > 768px both wrappers are shown, and when browser's width <= 768px the sidebar disappears.
It worked with me with CSS media queries, but after I added a jquery function to toggle showing/hiding the sidebar by a button, the CSS media queries stopped working after the first click.
Why did that happen, and how to get both of them to work?
JQUERY
var wrapper = $("#wrapper");
var sidebar = $("#sidebar-wrapper");
$("#btn-toggle").click(function(e) {
e.preventDefault();
collapse();
});
function collapse(){
if(sidebar.css("left") == "0px")
{
sidebar.css("left","220px");
wrapper.css("margin-left","220px");
}
else
{
sidebar.css("left","0px");
wrapper.css("margin-left","0px");
}
}
CSS (I'm using LESS here)
#wrapper{
.
.
margin-left: 220px; /* for left sidebar */
@media only screen and (max-width : 768px){
margin-left: 0;
}
}
#sidebar-wrapper {
.
.
left: 220px;
@media only screen and (max-width : 768px){
left: 0;
}
}