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

Customize the month template of the Kendo datepicker calendar popup in an AngularJS application

I've been working on adjusting the month template for an existing calendar popup. Initially, I used the code below: $("#" + attributeId).kendoDatePicker({ dates: jsonResult, weekNumber: true, month: { // template for dates in ...

What is the Angular alternative to control.disable when working with a QueryList?

I have encountered a challenge in my Angular form where I need to disable certain fields. The issue arises from the fact that many of the HTML tags used are customized, making it difficult to simply use the disabled attribute. To address this, I have opted ...

The jQuery bxSlider is failing to function properly in a responsive design

Anyone else experiencing issues with the jQuery bxSlider not functioning properly on a responsive layout? It seems to be working fine on larger screens. $(document).ready(function () { $('.slider1').bxSlider({ pagerCustom: '#bx-pager&apos ...

Unable to integrate an if/else statement into the JSON reply

Working with an API to retrieve data and display images from it. I am attempting to implement an if/else statement that will show photos if the search term yields results in the response, and display a message indicating no results if it does not. Curren ...

I'll show you how to implement custom fonts in TailwindCSS and NuxtJS!

Currently, I am in the process of developing a website using NuxtJS along with Tailwind CSS for styling. To incorporate my desired fonts, I have utilized the @nuxtjs/tailwindcss module. Despite applying the correct font-family through CSS, it seems that m ...

Ensuring Proper Alignment of Headers in CSS

I'm struggling to align my header correctly. Despite getting close in Internet Explorer, it looks terrible in Firefox with the code below. I've scoured forums for solutions but the more I try to fix it, the worse it looks. All I want is for heade ...

Enhance your text area by incorporating text using jQuery

There are moments when I become incredibly frustrated while attempting to perform basic tasks with Jquery. My issue lies in a textarea where users can input their own text, as well as select from predetermined phrases to include. The HTML setup is as foll ...

What is the best way to eliminate excessive margin-bottom on a floating image?

Is it possible to maintain the same margin at the right and bottom of an image when using <img> inside <p><img style="float:left">dummy text dummy text dummy text dummy text</p>? ...

What are the methods for managing HTML encoding with the python win32com package?

When using python, I employ win32com to convert word documents into HTML: from win32com import client as wc import os word = wc.Dispatch('Word.Application') doc = word.Documents.Open(wordFullName) doc.SaveAs(htmlFullName, 10) However, the ge ...

Encountering a jQuery error while attempting to initiate an AJAX request

I'm currently working on a project in SharePoint and I want to integrate JQuery to make an ajax call from the homepage. However, when I attempt to make the call, I encounter an error stating "Array.prototype.slice: 'this' is not a JavaScript ...

Horizontal scroll box content is being truncated

I've been trying to insert code into my HTML using JavaScript, but I'm facing a problem where the code is getting truncated or cut off. Here's the snippet of code causing the issue: function feedbackDiv(feedback_id, feedback_title, feedb ...

Retrieving Data from Outside Source using AngularJS

Is there a way to retrieve JSON-Text-Stream data from a specific URL (e.g. SOMEURL/ean.php?id=4001513007704)? The returned result typically appears as follows: { "product": { "ean_id": "4001513007704", "title": "Gerolsteiner Mineralw ...

Cloning a file input does not retain the uploaded file in the cloned input. Only the original input retains the uploaded file

Whenever I duplicate an input type file, I encounter an issue where the uploaded file from the duplicated input is also linked to the original input. It seems like the duplicate input is somehow connected to and taking files for the original one. This is ...

Local storage synchronization in progress, please hold on

Currently, there seems to be a synchronization issue between the local storage and the server. Countries, cities, and users are synchronized with the server separately through different Ajax calls. The problem at hand is that other JavaScript codes (such ...

What is the best way to automatically show scrollbars when a page loads using JavaScript?

How can I make the vertical scrollbar appear as soon as the page loads using javascript? I am currently using a jquery slide toggle animation that causes the vertical scrollbar to show up because it makes the page longer. However, when the scrollbar appear ...

Setting the Content-Type of a JavaScript file within a NodeJS application

I am facing an issue with opening a js-file on my NodeJS server, as it always specifies the .js file with a Content-Type of "text/html." My objective is to send user-input from an html form to a JavaScript file for performing calculations and later genera ...

Using the max-width property with Semantic UI Dropdown in ReactJS

I'm struggling to determine how to adjust the max-width of the Dropdown element in ReactJS. I attempted the following: .Menu Dropdown { max-width: 5rem !important; } Unfortunately, this did not work as expected. The dropdowns are taking up too m ...

Why is my AngularJS application triggering two events with a single click?

<button id="voterListSearchButton" class="serachButton" ng-click="onSearchButton1Click()">find</button> This button allows users to search for specific information: $scope.onSearchButton1Click = function() { var partId = $("#partIdSearch" ...

The issue of CSS transitions flickering in Internet Explorer

Can someone help me troubleshoot an issue with this code in Internet Explorer? CSS: .nav-fillpath a { width: 100px; height: 100px; position: absolute; display: block; bottom: 0%; left:0; right:0; color: #3e3e3e; margin ...

broadcast a video file from a Node.js server to multiple HTML5 clients at the same time

After researching online, I have been looking for a way to simultaneously stream a video.mp4 to multiple html5 clients. Despite reading various tutorials, I haven't found the ideal solution using nodejs. Do you have any suggestions or alternative met ...