Transitioning smoothly from mobile to tablet views with the sidebar activated in Semantic-UI

My HTML includes a pointing menu when the screen width exceeds 630px. Below 630px, it switches to a sidebar with a menu button:

<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 styling:

  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;  
    }
}

Upon resizing the screen to 630px or lower, the horizontal navigation bar menu disappears while a centered button appears. A screenshot can be found here

If I open the sidebar and resize the screen to a width greater than 630px, the menu items disappear but the sidebar remains visible. Another screenshot is available here

I am looking for a solution to ensure that the sidebar also disappears when open at resolutions over 630px.

EDIT:

While the sidebar is open, Semantic UI wraps everything else in a pusher class and adds a pushable class to the body. It utilizes the visible class to indicate the sidebar's status, prompting me to try:

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

Here is a visual representation of the DOM with the sidebar visible:

  <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>

Check out this jsfiddle and a runnable demo

Answer №1

Thank you for the recent updates to the Fiddle example. It seems that there is a conflict arising from the visual changes applied through both CSS and jQuery. Additionally, there is no existing code to hide the mobile menu when transitioning back to desktop screen size.

To address this issue, I propose making adjustments to the CSS to accommodate significant visual alterations based on each specific media query. Similarly, I suggest implementing corresponding modifications in the jQuery code by utilizing a listener to monitor screen width changes.

Here's a demonstration of these solutions in action: Fiddle

CSS Updates:

During instances where scripts are disabled, it is crucial for your CSS to handle essential functionalities to avoid potential page malfunctions. Prioritizing CSS over JavaScript can lead to optimized page loading times.

It is imperative to clearly define the behaviors of mobile and non-mobile elements when altering them using media queries. Specifically stating the states (showing/hidden) helps maintain consistency.

Mobile Layout:

@media all and (max-width:630px) {

/* Initially hide the main menu */
    #menu {
      display:none;
    }
/* Subsequently show the mobile menu */
    #m_menu {
        display:block;
    }
/* Lastly, display the mobile menu button */
    #m_btn{
        display:inline-block;
    }
}

Desktop Layout:

Reverse the adjustments made for the mobile version:

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

An alternative approach involves removing the media query encompassing desktop styles entirely, as any styles defined outside of (max-width:630px) will apply once 630px limit is exceeded.

jQuery Implementation:

Add a listener to detect shifts in width beyond the designated breakpoint. To account for precise breakpoints, it may be necessary to use a value such as 617 which aligns closely with the media query breakpoints.

Note that this functionality specifically targets screen resizing events triggered by user actions like adjusting browser dimensions or changing device orientation.

Within the conditional statement set up to activate only upon resizing to desktop size, forcibly hide the #m_menu.

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

For improved efficiency, exploring alternate methods for handling the resize event beyond using .resize() is recommended, as frequent firing of this event can impact page performance.

In my experience developing responsive websites, establishing a 'conditional JavaScript' snippet tailored to mirror the behavior of media queries has proven to be advantageous in similar scenarios.

Answer №2

I stumbled upon a resolution to the issue by utilizing enquirejs, a tool that can detect media queries being matched and unmatched.

  <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');
  });

Here is a JSFiddle link and a Runnable example showcasing this solution.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Issues arise with table-cell and table-row when implementing a navigation bar using the CSS table-layout property

Looking to create a dynamic navigation bar that adjusts to different screen sizes? On the left side, you want an undetermined number of links, while on the right side there should be a search button. The challenge is to ensure that all links have equal wi ...

Sending a jQuery variable to a static image source within a Django template

I have a Django view that returns a list of file names (which are .jpg images) as an Ajax response. My goal is to append each image to a div with the id grid. Here's my code: success: function(response) { var state = response.state; var piclist = ...

Is it possible to dynamically alter the text and contrast of your entire website?

In the realm of MVC 4 and Visual Studio 2013: I am pondering over how to dynamically change the text on my website with the simple click of a button. The same goes for adjusting contrast. Are there any plugins or techniques that could facilitate this proc ...

The timeline shows the movement of the jQuery object

I currently have a timeline bar that looks like this: <div class="plan-bar main-gradient"></div> https://i.stack.imgur.com/ybJar.jpg My question is, how can I make the red box move in real-time along this timeline bar? Thank you for your hel ...

"An issue with PHP not receiving any data from AJAX POST request

I've been working on creating a login using AJAX. The issue I'm facing is that when the AJAX function posts data to the PHP file, the data is empty when trying to retrieve it in PHP. Here is my AJAX posting code: function validateUser() { var ...

Merging two functions for concealing an element

Below are three functions that control the behavior of hovering and clicking on different elements: $(".redDiv").hover(function() { $(".blueDiv").show(); }, function() { $(".blueDiv").hide(); }); $(".blueDiv").hover(function() { $(this).show(); ...

`The header navigation is not responding to window resizing functionality`

I am currently developing a header navigation that consists of a logo on the left side, a profile icon on the right side, and some navigation links in the middle. A left slide menu has been implemented to trigger when the window width is less than 700px. ...

Looking for a way to manipulate the items in my cart by adding, removing, increasing quantity, and adjusting prices accordingly. Created by Telmo

I am currently working on enhancing telmo sampiao's shopping cart series code by adding functionality for removing items and implementing increment/decrement buttons, all while incorporating local storage to store the data. function displayCart(){ ...

The horizontal scrollbar is not displaying

(RESOLVED) Currently, I am developing a movie app that displays film titles through an Accordion feature using Bootstrap. Specifically, I have created an Accordion that showcases movies added to a favorites list. The intention is for this favorites secti ...

Verify for a class that does not exist

I am working on a script that updates the content of a div and increments its value by 1 $(function() { $.ajaxSetup({ headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content'), }, }); ...

How to retrieve the initial element of a specific tag using jQuery

As I am transitioning from standard JavaScript to jQuery in order to achieve cross browser compatibility, I am seeking guidance on how to rewrite certain functions in jQuery. Specifically, I am looking for the correct way to find the first element using jQ ...

Issue with Bootstrap collapsible menu not expanding when clicked

I want to make my collapsed navbar menu expand when clicked on a small screen. I've customized a Bootstrap navbar to split my links at each end of the bar. Right now, the links collapse when the screen size decreases, but clicking on the accordion but ...

The properties of the box model applied to unidentified inline boxes

Reference: CSS Specification Text directly contained inside a block container element (not within an inline element) is considered as an anonymous inline element. For example, in an HTML document like this: <p>Some <em>emphasized</em> ...

Guide on incorporating an external HTML file into an ASP.NET webpage

Is there a way to incorporate an external HTML file into a ASP.NET web page? In PHP, we typically use the include function for this purpose. I am looking for a similar solution in ASP.NET as well. My goal is to have a shared header HTML file that can be ...

The aoColumns functionality in datatables seems to be malfunctioning

Attached is the application picture, where the column ORDER ID is not showing and instead a PLUS sign is displayed. This causes all columns to shift one position to the right. ajax Every time I run the application, it displays the following error message: ...

Utilizing C# dynamic variable within inline CSS for an element (MVC)

I'm currently struggling with trying to generate a random color for an element in a cshtml view. No matter what I attempt, it just won't cooperate. When I include the @ symbol, the color property doesn't highlight as expected, leading me to ...

Issue with JavaScript function loading website homepage solely is functioning only for the first time

I am in the process of creating my own personal website. To ensure seamless navigation, I added a "home" button that, when clicked, triggers a JavaScript function called loadhomepage() to load the homepage. While this function works perfectly upon initial ...

The alert stubbornly refuses to close even when attempting to do so

I am encountering an issue with an alert in my Bootstrap 5 project. The alert does not close when I click on the "X" button. https://i.sstatic.net/pvCdm.png Below is the code snippet: {{#if success}} <div class="container p-4"> ...

Having difficulty processing information retrieved from an ajax request in jQuery

Upon making a request to the server and converting the retrieved data into a table format, I utilize jQuery's .html() function to display it on the webpage. However, despite the data being visible on the page, I encounter issues when attempting to man ...

Struggles encountered when choosing the initial visible item

I have a set of 3 tabs, each with its own heading and content. However, I only want to display the tabs that the user selects by checking the corresponding checkboxes. There are 3 checkboxes, one for each tab. Below is the code snippet: //Function to ...