I am trying to create a sidebar menu for my front-end, where the menu should show by default on the desktop, but start hidden on mobile. The 'active' class shown below handles the transition between show/hide. The sidebar works, when I click the 'hamburger' button on the phone, it displays or hides the menu just as it should.
CSS:
.sidebar {
transform: translateY(-100%);
transition: all .25s ease-in-out;
}
.sidebar.active {
transform: translateY(0);
transition: all .25s ease-in-out;
}
JS:
$("#sidebar-toggle").click(function () {
$(".sidebar").toggleClass("active");
});
The menu has 'active' class by default for the desktop, so it is showing unless user hides it, the problem appears when I try to hide the menu after page loads on mobile devices:
$(document).ready(function () {
if ( window.innerWidth >= 770 )
{ $(".sidebar").addClass("active"); }
else
{ $(".sidebar").removeClass("active"); }
});
However, when browsing the app on the phone, you can see the menu disappearing every time you refresh a page. How can I set the sidebar to be 'active' on desktops, but not on mobile devices by default without using JS?
UPDATE: since it looks like some people are having troubles understanding what's going on, I will try to clarify once again, now using just simple sentences:
I want my sidebar TO HAVE 'active' class by default on desktop (min-size > 770). I also want my sidebar NOT to have 'active' class by default on mobile (min-size < 770). This I can't do. I can only set the sidebar to be active ALL the time. I also can remove or add the class based on the screen size. The JS provided above does this. However this happens after the page loads. The result (on a mobile) is a sidebar that appears on every refresh/redirect, and then transitions. I do not want it to be active on refresh/redirect and then transit away. I want it to be inactive from the start on the phone. But active on the desktop.
UPDATE2: I really hope this is easier to understand: I want to create the sidebar with 'active' class on desktops, but WITHOUT the 'active' class on mobiles. I do not want to start with 'active' class and then remove it with JS after the page loads, that's what I do now. I want the sidebar to start without the active class - BUT only on mobile.