Tips on how to prevent body scrolling when a specific button is pressed

I have created an HTML page with a menu and content section. Everything is working perfectly on desktop, however, I am facing an issue on mobile devices. When the menu is clicked on mobile, the body content overlaps with the menu items while scrolling (the content scrolls along with the menu).

I tried using `overflow: hidden` and `position: fixed`, but it resulted in making the entire page non-scrollable even when the menu button is not clicked.

Could someone kindly assist me with this matter?

Here are the steps I have taken:

In my JS file:

 $(document).ready(function () {
    $("#mobile-toggle").click(function () {
        $('body').css('overflow', 'hidden'); 
    });
});

My mobile toggle button code:

<button id="mobile-toggle" class="mobile-toggle">
    _('mobile-toggle')<span></span>
</button>

Although the button disables scrolling upon click, it does not work as intended on mobile devices.

Answer №1

To ensure your website looks great on all devices, consider updating your CSS to make it responsive. Check out this helpful guide for more information: Responsive Design

Answer №2

Remember to also include the html element in your selector:

$('body,html').css('overflow', 'hidden'); 

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

Reading a JSON file using Javascript (JQuery)

Struggling to figure out how to handle a JSON file using JavaScript. Here are the details : { "streetCity": { "132":"Abergement-Clemenciat", "133":"Abergement-de-Varey", "134":"Amareins" } } I am attempting to access ...

Determine the total hours and minutes elapsed between two specific dates and times

Looking for some assistance here. I have a form where users need to input a start time and end time of an incident. After entering this information, they would manually calculate the duration between the two date times. I am attempting to streamline this p ...

Bringing joy to a JS library: Embracing both Node and the Window

I have developed a JS library that I want to convert into a Node module for use with Node.js. This library extends the Canvas context API and relies on the use of getImageData(). Therefore, it begins with a defensive check to ensure compatibility: if (wi ...

Interactive Jquery Slideshow for Mobile Devices

Help needed to arrange images in a slideshow format! The images are currently scattered on the webpage and causing confusion. Thank you in advance for any assistance provided. Below are the HTML, JavaScript, and CSS codes that need to be fixed: HTML < ...

Tips for creating a dynamically responsive eye icon placement

When the screen width is less than 991px, the content is centered but the eye icon that is absolutely positioned goes off-center. This issue only occurs on devices with a small or medium screen size. The reason for using absolute positioning on the icon i ...

Achieving Vertically Centered Buttons with CSS

Trying to center-align a simple input button within a table cell has been quite the challenge. The following is the current markup: <table width="500" border="1"> <tr> <td width="390">XXXXXXXXX</td> <td width=" ...

Finding the value of an item within a JSON sub-array based on the most recent date that falls within a specified timeframe

I am currently developing a billing application using Angular. The items array that I am working with is structured like this (showing only one item here):- items: [{ _id: "5ee2298f59ce2e1747cb5539" createdAt: "2020-06-11T ...

What is the best way to ensure your background image appears proportional?

I'm having an issue with the background image on my login page - it doesn't look like the original photo: Take a look at the screenshot to see the problem with the width of the image: https://i.sstatic.net/qb9u0.jpg Here's the HTML/jsx cod ...

Make sure to maintain the hover effect even after clicking

How can I make my div retain the hover effect even after clicking on it? Here is the code snippet: #work_btn{ position:absolute; top:60%; left:60px; color:grey; background: -webkit-linear-gradient(0deg, #ff956c, #ff3644); -webkit-background-clip: text ...

After hovering over the box, we can observe the :after pseudo

Encountered an issue where the box fails to appear when hovering over the :after element. HTML CODE: <div class="top_line_dropdown"> <span class="logged_in_user"> <a href="/cdn-cgi/l/email-protection" class="__cf_em ...

The issue of Django tooltip translations failing to work when there are white spaces present

While translating an html tooltip using i18n, I encountered a issue where the text does not display properly when placed between translation tags {% translate ... %} unless spaces are removed. Here is the code without the translate tag: <th scope=" ...

Missing transitive dependencies have been identified within the node_modules directory when using npm

In my software development journey, I am working on two npm projects - one called X and the other called Y. Let's say that X relies on angular and has it specified as a dependency in its package.json file. To establish a connection between X and Y, I ...

When using videojs, I have the ability to include a Text Track event handler, however, there is currently no option to remove it

I implemented a listener for the 'cuechange' event on a Text Track and it's functioning correctly. However, I am unable to figure out how to remove this listener. I have attempted the instructions below to remove the listener, but it continu ...

Display or conceal HTML based on the user's location within the webpage

I'm trying to implement a dynamic horizontal navigation bar on my website that appears and disappears based on the user's position on the page. However, I'm facing some challenges in getting it to work correctly. Here is the snippet of code ...

Request body not parsed properly, req.body not defined

I am currently working on a Post route to add new products to my MongoDB database. router.post('/addprod', mid.reqAdmin, function(req, res, next){ if(req.body.category && req.body.name && req.body.price && req ...

Error: The OrbitControls function is not recognized in THREE.JS

const scene = new THREE.Scene(); const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000); const controls = new OrbitControls(camera); camera.position.set(200, 0, 0); controls.update(); const geometry = new THREE.S ...

How about creating a fresh variable in Assemble or merging two comparison helpers together?

Is it possible to create a new variable within a partial in Assemble (assemble.io) or combine two comparison helpers? For example: {#is somevar "yes" || anothervar "no"} In my partial, I have HTML that should only display if one of two different variable ...

Generating Speech from Text using jQuery API in HTML

Can a website be created to detect textbox text upon longClick by the user, and function across various browsers? The site should also have mobile compatibility. Appreciate any help! ...

A JavaScript pop-up box with two windows

Struggling to create two separate dialog boxes using this code. The issue is that when I add the code for the second dialog box, it only appears if the first one does too. Here's the code for the first dialog: function showPopUp(el) { var ...

Using jQuery to extract the value of an object from an <li> element and updating the class attribute of the <li> element

There is a div element: <div id="ddDistr" class="searchBoxSortDistrict" tabindex="1"> <ul id="ddd"> </ul> </div> I have inserted HTML from json into it: $("#ddd").html(" "), $.each(dis, function( index, value) { $("#dd ...