The off canvas menu remains visible even while adjusting the window size

Can someone please help me? I am currently working on an off-canvas menu (you can find it here). The issue I am facing is that when the mobile menu is open and a user resizes the window, the content still retains its transform property and overlay. How can I reset everything to their initial parameters when the window is resized? Should I use JavaScript to check if the window resize is greater than 768px (my breakpoint) and then hide the overlay and transform the content back?

This is my current JavaScript code:

 $(document).ready(function() {
  $('#nav-toggle').click(function(){
    if($(this).is(":checked")) {
      $('.content-wrap').css('transform', 'translateX(80%)');
    } else {
      $('.content-wrap').css('transform', 'translateX(0)');
    }
    $('body').toggleClass('overflow-hidden');
    $('#c-mask').toggleClass('is-active');
  });

  $('#c-mask').click(function() {
    $('#overlay').fadeOut('slow');
    $(this).removeClass('is-active');
    $('#nav-toggle').prop('checked', false);
    $('.content-wrap').css('transform', 'translateX(0)');
  });
});

Answer №1

Utilize the $( window ).resize(function() method to monitor the size of the window and turn off the offcanvas feature

  $( window ).resize(function() {
if ($(window).width() > 768) {
$('#overlay').fadeOut('slow');
$('#nav-toggle').prop('checked', false);
$('.content-wrap').css('transform', 'translateX(0)');
}
});

Answer №2

Give this code snippet a try

$( window ).resize(function() {
  if($( window ).width() > 768) {
    $('.content-wrap').css('transform', 'translateX(0)');
  }
});

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

Creating a dynamic side navigation menu that adjusts to different screen sizes

I have been working on a project's webpage that was initially created by another Software Engineer. After he left, I continued to work on the webpage. The issue I encountered is that he preferred a side menu over a navbar using the left-panel and righ ...

Firebase Lists versus Objects: A comparison of data structures

Seeking some clarification regarding Firebase lists. I'm attempting to access multiple lists within a single object in order to iterate through them. The structure of the object is as follows: user : { playlists: {}, // List 1 joined: {}, ...

Exploring the deeper significance behind Lucid's "Term" category

Lately, I've been delving into Haskell and experimenting with creating a basic website using Servant and Lucid. Right now, I find myself at that familiar stage where "My code works, but I have no idea why". I decided to include a Bootstrap button in ...

Is it possible to pass a parameter to a PHP controller using JavaScript without relying on jQuery or AJAX?

Is it possible to achieve the task at hand? That's the main question here. My goal is to extract data from a popup window and then, upon closing it, send this extracted content to a PHP controller for further processing. I'm facing conflicts wi ...

Running jQuery code within an AngularJS directive's templateUrl

I am facing an issue where my jQuery code, embedded within an AngularJS directive HTML template, is not functioning as expected. Neither alert("testA"); nor alert("testB"); are being triggered in the provided code snippet. I suspect that the problem lies ...

Troubleshooting: Else block not functioning as expected within a React JS map function

I have a notification feature that makes an API call every 10 seconds to display an alert based on the response. However, I'm encountering an issue where the div is not being rendered properly. The div should be displayed based on certain conditions w ...

Error code 10062 occurred on the Discord Bot while verifying the channel name during the processing of a user-sent slash command

I've been developing a Discord bot using JavaScript for a study server with my friends. Currently, I'm working on a feature that allows users to add phone numbers but only in specific channels. Everything was functioning properly until I implemen ...

Is it possible to combine Bootstraps responsive utilities into a single Less file?

Looking to implement the show/hide functionality from Bootstrap without needing the entire framework. I attempted including just the responsive-utilities.less file, but encountered errors with lessphp. Does anyone know of a website where I can find this v ...

Update the login link to display as "logout" using PHP within a Bootstrap framework

Using Bootstrap, I have created a simple jobs board design internally within my workplace. Unfortunately, I am unable to share the code externally. The design is coded in Bootstrap 4, HTML, and CSS. The header part of the design has been separated so that ...

Manipulate images in real-time and insert custom text using JavaScript/jQuery

Within my possession is an image depicted above. My objective revolves around dynamically altering the values present at the occurrences of L, A, and B; to achieve this, I must eliminate or conceal L, A, and B while substituting them with numerical equiv ...

Trouble with displaying pagination within an iframe

I am facing an issue with the pagination in iframe where the height of the iframe does not increase according to the content when I navigate to the next page. Please see the issue demonstrated here: http://fsportal.us-east-1.elasticbeanstalk.com usernam ...

Steps for fixing the Ajax-Zoom issue: $.axZm is either invalid or not defined

Hello there! I'm currently attempting to run the sample codes from AJAX-ZOOM on my local server, but I keep encountering the following error in the console: "Uncaught TypeError: Cannot read property 'axZm' of undefined." Any ideas on what mi ...

Interactive Hover-Dropdown Effect on a Styled Division

Recently, I encountered an issue with my drop-down menu where it goes behind another div when hovered over. This causes the drop-down menu to not be fully visible. I tried adjusting the z-index thinking it would solve the problem, but unfortunately, it di ...

the speed of accessing an array in JavaScript

Assuming there is a javascript array1 with 10,000 elements, what would be the time complexity of: var array2=new array(); array2.push(array1); and what about the time complexity of var object={}; object['array2']=array1; Are both operatio ...

Execute a JavaScript function once the server-side validation has successfully been carried out

I am facing a challenge with a lightbox that contains a small form (2 fields) within an UpdatePanel. My goal is to close this lightbox using JavaScript when the 'Save' button is clicked. The complication arises from the presence of a server-side ...

Is it possible to add an element to an array field in a document using Mongoose/Node?

Currently, I'm working with a Mongoose schema that includes an array field designed to hold multiple strings: const exampleSchema = new.mongooseSchema({ field1: String, field2: String, field3: [String] }) My goal is to configure the ...

Is there a way to efficiently return an array of object and nested object keys with their full paths through recursion

My grasp of JS is still in its early stages, so understanding how the stack and recursion function can be challenging for me at this point. The task at hand involves displaying an array of object keys, including the parent object name if the object is nest ...

Printing an Iframe using Safari print with Javascript results in an empty output

After researching the issue of printing blanks in Safari, I discovered that a white flash occurs before the print dialog can grab the content of the iframe. All my javascript works perfectly in every browser except for Safari. When attempting to print, it ...

The way images appear can vary between desktop and mobile devices

I am in the process of creating a photography website with a simple goal of displaying images down the page one after another. However, I am encountering issues with uniform display across various desktop and mobile platforms. The site appears perfect on i ...

Reveal the MongoDB database connection to different sections within a Next.js 6 application

Currently developing an application using Next.js v6, and aiming to populate pages with information from a local mongodb database. My goal is to achieve something similar to the example provided in the tutorial, but with a twist - instead of utilizing an ...