Semantic-UI Transistioning from Mobile to Tablets with Open Sidebar

This is my html where I have a pointing menu when the screen width exceeds 630px and a sidebar with a menu button when the screen width is less than 630px :

 <nav class="ui inverted sidebar menu vertical slide out" id="m_menu"> <a href="index.php" class="item"> <i class="home icon"></i>Home </a> <a href="about.php" class="item"> <i class="user icon"></i>User </a> <a href="portfolio.php" class="item"> <i class="archive icon"></i> Portfolio </a> </nav> <div id="menuDiv"> <nav class="ui inverted fluid four item pointing menu" id="menu"> <a href="index.php" class="item"> <i class="home icon"></i>Home </a> <a href="about.php" class="item"> <i class="user icon"></i>User </a> <a href="portfolio.php" class="item"> <i class="archive icon"></i> Portfolio </a> <a href="contact.php" class="item"> <i class="message icon"></i> Contact </a> </nav> <div class="ui labeled icon button black" id="m_btn"> <i class="icon list layout"></i> Menu </div> </div> <img src="http://paulandtwyla.files.wordpress.com/2010/02/table-chairs-2.jpg" alt="Table With Chairs"> 

This is my css:

  body{ margin:0; padding:0; font-family:"helvetica",arial; } #menuDiv{ margin:40px 40px 0 40px; text-align:center; } #menu:{ max-width:1024px; margin:0 auto; } #m_btn{display:none;} img{ width:100%; height:auto; position:absolute; top:0; z-index:-1; } @media all and (max-width:630px) { #menu{display:none;} #m_btn{ display:inline-block; } } /*fails to fix sidebar sidebar showing @width>=631px*/ @media all and (min-width:631px) { /*suggested on tutsplus,no change on applying*/ body.pushable{margin-left:0 !important;} #m_menu.visible{ display:none; } } 

When I change the screen size to 630 pixels or lower, the menu of the horizontal navigation bar disappears and a button appears in the center of the page. Screenshot link here

I open the sidebar and resize the screen to a width of more than 630 pixels, where it removes menu items on the sidebar, but not on the sidebar itself. There is a screenshot here

How can I make sure the sidebar disappears even if it is open with a resolution of more than 630 pixels?

EDIT:

When the sidebar is open, the semantic ui transfers everything else to the pusher class and adds the pushable class to the body , it uses the visible class to provide you with information about what the sidebar does, this is the reason for trying

  @media all and (min-width:631px) { /*suggested on tutsplus,no change on applying*/ body.pushable{margin-left:0 !important;} #m_menu.visible{ display:none; } } 

This looks like a DOM with a visible sidebar:

  <body class="pushable"> <nav class="ui inverted sidebar menu vertical slide out uncover left animating visible" id="m_menu"> <a href="index.php" class="item"> <i class="home icon"></i>Home </a> <a href="about.php" class="item"> <i class="user icon"></i>User </a> <a href="portfolio.php" class="item"> <i class="archive icon"></i> Portfolio </a> </nav> <div class="pusher dimmed"><div id="menuDiv"> <nav class="ui inverted fluid four item pointing menu" id="menu"> <a href="index.php" class="item"> <i class="home icon"></i>Home </a> <a href="about.php" class="item"> <i class="user icon"></i>User </a> <a href="portfolio.php" class="item"> <i class="archive icon"></i> Portfolio </a> <a href="contact.php" class="item"> <i class="message icon"></i> Contact </a> </nav> <div class="ui labeled icon button black" id="m_btn"> <i class="icon list layout"></i> Menu </div> </div><img src="http://paulandtwyla.files.wordpress.com/2010/02/table-chairs-2.jpg" alt="Table With Chairs"></div> </body> 

Here is jsfiddle and runnable

+7
jquery html css responsive-design semantic-ui
source share
2 answers

I found a solution to a problem that uses enquirejs , which can be used to listen for matches and unsurpassed media requests.

  <script src="//rawgit.com/weblinc/media-match/master/media.match.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/enquire.js/2.1.2/enquire.js"></script> var query='all and (min-width : 631px)'; enquire.register(query,function(){ $('#m_menu').sidebar('hide'); }); 

It is fiddle and it is runnable with it.

0
source share

Thanks for updating the Fiddle example, the problem is that there is a conflict between the visual changes performed by CSS and jQuery. In addition, there is no code that would not force the mobile menu to hide after changing the screen size to the desktop.

The solution I came across is to first configure CSS to satisfy the basic visual changes for each media query , and then do the same for jQuery changes using a listener that checks for screen width changes.

Here is the fiddle of this in action

CSS

If scripts are disabled, your CSS should always handle the basics of what you want to achieve. Otherwise, the page may break. It also has the huge benefit of less page load (never use JS when CSS can handle it).

You need to clearly indicate what should happen to any mobile / non-mobile element, if you change it at all using media queries. Given that we are talking about something that has states: showing/hidden , you should indicate what these states refer to:

For your mobile layout:

 @media all and (max-width:630px) { /* First, hide the main menu */ #menu { display:none; } /* then display the mobile menu */ #m_menu { display:block; } /* lastly, show your mobile menu button */ #m_btn{ display:inline-block; } } 

Desktop layout

In principle, undo everything you have done for the mobile version:

 @media all and (min-width:631px) { /* Hide the mobile menu */ #m_menu { display:none; } /* Unhide the regular menu */ #menu{ display:block; } /* Lastly, hide the mobile menu button */ #m_btn{ display:none; } } 

Alternatively, you can completely remove the media query around the desktop styles, since any styles specified outside (max-width:630px) will be applied regardless of whether the value 630px is exceeded.

JQuery

You can then use the listener to check when the width changes after the checkpoint you have selected. It’s hard to grab the exact width since “630” shot too wide, so I went with 617 when it compared the media query breakpoints.

Please note that this only applies to changing the screen size, i.e. the user drags his browser size or changes the orientation of his device from portrait to landscape or vice versa.

Inside the if , which will only work when the screen is resized to the size of the desktop, we can forcibly hide #m_menu .

 $(window).resize(function() { // Detect if the resized screen is mobile or desktop width if($(window).width() > 617) { console.log('desktop'); $('#m_menu').sidebar('hide'); } else { console.log('mobile'); } }); 

Probably the best way to do this last part, since .resize() can fire many times per second and can noticeably slow down the page.

In my experience of creating sensitive sites, it’s very useful to have a fragment of “conditional javascript” that is specially configured for this kind of script, which behaves the same as media queries .

+2
source share

All Articles