"Upon clicking, toggle the addition or removal of the overflow hidden

I'm currently working on a function to add and remove the CSS property .css('overflow-y','hidden') using an onclick event, which I have successfully implemented. However, I am encountering an issue when attempting to remove this CSS using another onclick event triggered by a different element.

The concept involves a modal window (utilizing Twitter Bootstrap 2.3) that displays some data. When the user clicks on a button within the modal, the CSS is applied to prevent scrolling of the website. The problem arises when the modal is closed and the overflow-y styling remains, preventing the page from scrolling properly.

I have developed the code up to this point but seem to be stuck and unable to identify where I may be making a mistake. I would greatly appreciate any assistance on resolving this issue, as well as any advice to avoid similar problems in the future.

Thank you!


   <script type="text/javascript">
     $('#myModal').modal('hide')             // initializes and invokes show immediately</p>
     $('.note').delay(10000).fadeOut('slow');

     $(document).ready(function() {
         var $html = $('html');
         var $button = $('.container > .btn-primary');
         var $modal = $('.modal-backdrop');

         $button.on('click', function(e) {
             $html.css('overflow-y', 'hidden');
             if ($html.attr('style')) {
                 alert('WORKS!');
             } 
             else {
                $modal.onclick( function() {
                    $html.css('overflow-y','scroll'); 
                 });
             };
         });

     });
  </script>

Answer №1

To easily show or hide overflow, organize your CSS in a class and leverage jQuery's .toggleClass() function.

For a straightforward illustration, refer to this example: http://jsbin.com/towiyaqa/1/

Answer №2

This is the way to implement it:

$btn.click(function(event) {
    $body.css('overflow-y', 'hidden' ? 'scroll' : 'hidden');
    event.preventDefault();
})

Answer №3

Here is a possible solution to the issue at hand:

$(document).ready(function() {
               var $html    = $('html');
               var $button = $('.container > .btn-primary');
               var $modal   = $('.modal-backdrop');

                $button.on('click', function(e) {
                    $('.note').delay(10000).fadeOut('slow');
                    $html.css('overflow-y', 'hidden');
                    if ($html.attr('style')) {
                        console.log("overflow-y: hidden added");
                    }
                });

                $('#myModal').on('hidden.bs.modal', function () {
                    // do something…
                    console.log("fires myModal");
                    $html.css('overflow-y','scroll'); 
                });
           });  

        </script>

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

The CSS background image in PNG format displays a white line at the beginning of the transparency

How can I remove the white border that appears on PNG images with transparency when used as a background with CSS on retina devices? .background { background: url('../image.png') no-repeat; } ...

Traverse through an object in ReactJS

Having trouble iterating over an object in my ReactJS project and facing some content display issues. Check out this fiddle for reference - http://jsfiddle.net/8e039Ltw/ Here's the ReactJS code snippet:- class TodoApp extends React.Component { ...

What steps do I need to take to generate a terrain similar to this using Three.js?

Trying to construct a terrain based on a heightmap with an enclosed bottom layer. Refer to this example for clarification: The current function for generating the terrain is as follows: var img = document.getElementById("landscape-image"); var numSegment ...

Designing a web application with Angular2

As a newcomer to Angular2, I have recently started working on creating a simple hello world application. I have come across the concept of Modules and Components in Angular2. My main source of confusion lies in how to properly design an Angular2 applicat ...

The alignment of divs on the right side is off-kilter after removing margins from the middle columns

While utilizing the Blueprint CSS framework, I encountered a challenge with removing the margins between columns. Removing these margins caused an issue where a div spanning all columns appeared wider than the combined width of the divs on rows where margi ...

Navigating within fixed-positioned divs

I'm attempting to create something similar to the layout seen on: The desired effect is to have fixed content on the right side, with only the left side scrolling up to a certain point before the entire page scrolls normally. However, in my implement ...

What is the process for creating a symbolic link from a project's node_modules directory?

Recently, I've been learning how to build a Password Manager using React, Node.js, and MYSQL by following a tutorial. However, I encountered an issue where the file /EncryptionHandler was located outside of the src/ directory of my project. Even thoug ...

Is NextJS rendering solely on the server, or is it capable of rendering on both

Within my users.js JSX file, I have an exported component that looks like this: ... return <MainContainer keywords="users"> export default Users During the process of SSR/SSG, the browser displays the users HTML (comprising of <li> t ...

How to place a button in the bottom center of a panel using asp.net

After learning about centering a button at the bottom-center inside a div, it became clear that it's similar to centering a button within a panel. I managed to achieve this by adding the following CSS to the button: position:absolute; margin-left ...

Unable to retrieve a single item in NextJS

Struggling with fetching a single item in NextJS const PRODUCT_API_BASE_URL = "http://localhost:8080/api/v1/products/"; export const getStaticPaths = async () => { const res = await fetch(PRODUCT_API_BASE_URL); const data = await res.json(); ...

Moving from one page to another

I am attempting to create a transition effect between sections within a single-page application. All the sections are contained on the same page, with only one section displayed at a time while the rest are set to display none. When a specific event is tri ...

Third Party Embedded Video requiring SSL Certificate

I am currently facing an issue where I am trying to embed videos from a website that does not have an SSL certificate. This is causing my own website to appear insecure when the page with the embedded video loads, despite the fact that my website has its ...

Set default values for input fields based on selected options in php and mysql

I need help creating a form that will submit details when an option is selected from a database. The MySQL table has the following fields under USERS : [email], [age], [name]. I want to automatically fill in the values of other input fields when a user s ...

PHP regular expression that identifies a specific HTML element

Similar Question: Effective ways to parse HTML using PHP I am currently creating a custom WordPress template for a client that requires a PHP function to extract specific HTML tags as defined by the function. For instance, if I input "div" into the f ...

What strategies can be used to stop elements from reverting to their original state?

Hey there, I'm currently experimenting with animation and trying to create a cool two-step effect: 1. elements falling down and 2. pulsating on click. However, I've run into an issue where after the clicking action, the elements return to their o ...

Transforming a JSON string containing multiple arrays into a JavaScript object

My JSON file is structured as follows: { "items":[ { "username":"abc", "orderID":"1234", "commentHistory":[ { "comment":"Comment Date: 2016/12/09 13:44:23" }, { ...

Taking data input from three textboxes and storing them in a PHP array

In my HTML form, I have 3 text input fields and one submit button. Each text field is intended for entering the name of an animal, which should then be saved into a PHP array. Here is the structure of my HTML form: <form action="Test.php" method="post ...

CSS animations are not functioning properly on disabled Bootstrap buttons

One issue I'm facing is with a BootStrap button that I'm rendering in React. The button has a property or a condition that determines if it's disabled: <Button bsClass="fill" disabled={!props.purchaseble} onClick={conso ...

Live search feature for multiple input fields using jQuery for an array of elements

I am currently trying to implement a jQuery live search feature for multiple input fields. The live search works perfectly fine with just one input field (without an array), but when using multiple search fields with an array, nothing seems to happen. ...

The A-frame inspector tool is not very useful within the Glitch platform

Just want to preface this by saying that I am completely new to both a-frame and Stack Overflow. Lately, I've been experimenting with A-frame templates similar to the one linked here: When I pull up the inspector for the example above (ctrl-alt-i), ...