Struggling to resolve a glitch in the mobile navigation that requires attention either through JavaScript or CSS coding

I'm having an issue with a mobile navigation menu I created. It's a simple hamburger icon that, when clicked, opens a fullscreen menu. The problem is that I want to prevent scrolling when the overlay is visible. I tried using this code:

$('body').bind('touchmove', function(e){e.preventDefault()});

However, this doesn't toggle within the click event. How can I make it so that scrolling is disabled when the menu is open and enabled again when the menu is closed? Here's the full script:

$(document).ready(function () {
  $(".icon").click(function () {
    $('body').bind('touchmove', function(e){e.preventDefault()});
    $(".mobilenav").fadeToggle(500);
    $(".top-menu").toggleClass("top-animate");
    $(".mid-menu").toggleClass("mid-animate");
    $(".bottom-menu").toggleClass("bottom-animate");
  }); 
});

Answer №1

Check out this helpful link:

Learn how to use jQuery to disable page scrolling programmatically This code is effective

$(document).ready(function(){
   $("button").click(function(){
      $('html, body').css({
         overflow: 'hidden',
         height: '100%'
      });
      $("p").toggleClass("main");
   });
   $('html, body').css({
   overflow: 'auto',
   height: 'auto'});  
});

Answer №2

 $(document).ready(function () {
  $(".icon").click(function () {
    $('body').bind('touchmove', function(e){e.preventDefault();});
    $(".mobilenav").fadeToggle(500);
    $(".top-menu").toggleClass("top-animate");
    $(".mid-menu").toggleClass("mid-animate");
    $(".bottom-menu").toggleClass("bottom-animate");
  }); 
  $('body').unbind('touchmove'); 
});

Would you be willing to give this a shot?

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

"Function.click operates successfully when no arguments are given, but encounters issues when arguments are supplied

I had a function that was functioning correctly: $('#buttFurniture').click(onFilterCLick); However, when I decided to pass arguments to the function, it stopped working: $('#buttFurniture').click(onFilterCLick(arrOutput, arrFurniture ...

Would it be considered improper to apply ID's to DOM elements for the purpose of automation, as it may transform their CSS classes into individual entities?

Just thinking about the best approach for Automation (such as Selenium). I've been advised against using IDs on elements, as it could result in JS errors (in case of duplicate IDs) and make CSS classes unique. While I see the point, not having IDs can ...

I'm looking to customize my Shopify store so that the progress bar is consistently placed directly underneath the "Add To Cart" button for every theme. Is this possible to achieve?

How can I add a progress bar below the "Add To Cart" button consistently across different themes? Each theme has a different id and class for the button. Is there a way to dynamically handle this? Additionally, is it possible to load Bootstrap using a scri ...

Trouble with Click event not working in Electron and mouse cursor not changing when hovering over an HTML button?

I'm in the midst of a project that involves using the electron framework. Within my project, I have an HTML file, a CSS file, and a Javascript file. The issue at hand is that when I hover over a button, the expected hand pointer icon fails to appear d ...

Tips on passing a User defined object to a controller using ajax/jquery:

This is an example of my HTML code: <input type="text" name="name" id="name" /> <input type="text" name="age" id="age" /> <input type="button" id="button" value="Submit"/> Here is the corresponding JavaScript code: <script t ...

What is the best way to ensure that the draggable element remains visible on top of all other elements on the screen while being dragged?

Currently, I am working on implementing the drag and drop feature in SAPUI5 Table and Tree Table. In my project, there is a table containing names that need to be draggable, and a Tree Table where these names should be droppable. To achieve this function ...

Load data from a MySQL Database into a DataTable

I am currently in the process of developing a CRUD application. With a large dataset stored in a MySQL database, my intention is to utilize a JQuery DataTable instead of manually creating a table. However, the issue I am facing is that while the table appe ...

Switch the checkbox to a selection option

Is there a way to switch the selection method from checkboxes to a dropdown menu? $("input[name=koleso]:first").prop("checked", true); $('body').on('click', '.koleso label', function (e) { koles ...

What is the best way to position an <a> tag in the top right corner of a card using Bootstrap?

I own a card that features an image in the background. <div class="col-12 col-sm-8 col-md-6 col-lg-4 mt-4"> <div class="card"> <img class="card-img" style=" filter: brightness(75%);" src=& ...

What could be causing this Bootstrap column to misbehave?

Why is the content stacking within each list item instead of being on the same line? I've attempted using floating and adjusting the position, but nothing seems to be working. How can I resolve this issue? .icon-left{ float:left; padding: 1 ...

Capture the mouse's coordinates using the mousedown and mousemove events

When the user clicks on the slider, my jQuery script tracks the mouse coordinates. However, even after releasing the mouse button, the function continues to run. $("slider").mousedown(function(){ $(this).mousemove(function(e){ console.log(e.cli ...

Learn how to dynamically apply a CSS attribute for a set period of time using jQuery and automatically revert it back to its original state after 2 seconds

Is there a way to apply a CSS attribute to a specific element (like a div) for only 2 seconds? Here is what I have tried so far: HTML: <div class="custom-div">bar</div> <input class="button" type="button" value="press me" /> JQuery: $ ...

Unusual phenomenon of overflow and floating point behavior

HTML and CSS Output Example: <div id="float_left"> DIV1 </div> <div id="without_overflow"> DIV2 </div> CSS Styling: #float_left{ float: left; width:200px; background-color: red; } #wit ...

Problems with Jquerymobile's $.mobile.changepage Function

Currently, I am utilizing the $.mobile.changePage function in Jquerymobile to transition between pages. However, I have encountered an issue with double transitions occurring when moving between pages. It's puzzling why this is happening. If anyone e ...

Unable to reach the object property while using the $.each method

Upon receiving an object from an ajax request, I am using $.each to iterate through all the appointments in the object. However, I am encountering an issue where the object appears to be undefined within the $.each loop. Here is the code snippet for my req ...

Issue with JSONP success function not being triggered

Here is an example of an Ajax call being made to a different domain: <script> $.ajax({ url: 'url?callback=?', dataType: 'jsonp', success: function (data) { alert(data[0].DeviceName) ...

Tips for enhancing the width of the extrude shape in the x and z axes using Three.js

After creating a shape using extrude geometry, I found that I needed to increase the thickness along the x and z axes. Although I used bevelThickness to increase the thickness along the y axis, I still need to adjust it further. For reference, please see ...

Creating an image slideshow with a looping JavaScript array: Step-by-step guide

One issue with the program is that when it runs, only the last array value image is displaying on the screen while the other images are not. Below is the HTML code: <img id="mg" alt="image not found"> And here is the JavaScript code: var images=[ ...

Mobile Menu in wordpress stays visible after being clicked

I recently created a one-page layout that includes some links. However, when I view the site on my smartphone and open the main menu using the button to click on a link (which scrolls within the same page), I noticed that the mobile menu remains visible a ...

I am having difficulty combining 2 HTML pages without the output becoming distorted

Having encountered issues when combining two HTML pages, one for a navbar and the other for a contact form in my Django project. The resultant single page appears distorted as shown in the image below. I attempted to use sections but it does not seem to re ...