Display modal scroll functionality when a modal window is activated

Can someone explain to me why my solution isn't working? I'm trying to hide the scroll on my page when a modal is opened and allow scrolling within the modal. It would also be great if there could be a slide up & down effect.

I attempted to achieve this using JavaScript, but it's not functioning as expected:

$(function(){

$('.modal').click(function() {
$('body').css({'overflow': 'hidden'});
});


$('.close').click(function() {
$('body').css({'overflow': 'scroll'});
});

});

Here is my code:

<a class="modal" href="#openModal1">Box 1</a>

<div id="openModal1" class="modalDialog">
<a href="#close" title="Close" class="close">x</a>

<div class="middle">
Modal Box 1
This is a sample modal box that can be created using the powers of CSS3. You could do a lot of things here like have a pop-up ad that shows when your website loads, or create a login/register form for users.
</div>
</div>

<a class="modal" href="#openModal2">Box 2</a>

<div id="openModal2" class="modalDialog">
<a href="#close" title="Close" class="close">X</a>
<div class="middle">
Modal Box 2
Box 2
yadda yadda
</div>
</div>


.modalDialog:target > body { overflow:hidden; }

.modalDialog {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    background: rgba(255,255,255,1);
    z-index: 99999;
    opacity:0;
    -webkit-transition: opacity 100ms ease-in;
    -moz-transition: opacity 100ms ease-in;
    transition: opacity 100ms ease-in;
    pointer-events: none;
}

.modalDialog:target {
    opacity:1;
    pointer-events: auto;
}

.modalDialog > .middle {
    width: 80%;
    position: relative;
    margin: 10% auto;
    padding: 10px 20px;
    background: #fff;
    overflow:scroll;
}


.close {
    background: #fff;
    color: #000;
    line-height: 0px;
    text-align: center;
    position: absolute;
    right: 10px;
    top: 20px;
    width: 24px;
    text-decoration: none;
}

You can view the full example on this jsfiddle.

Answer №1

It seems like you're attempting to utilize jQuery in your code, but it appears that you may have forgotten to include it. Take a look at the snippet below for a different approach than what you have tried. Instead of directly modifying CSS properties, I am adding and removing classes.

$(function(){

$('.modal').click(function() {
$('body').addClass('scroll');
});


$('.close').click(function() {
$('body').removeClass('scroll');
});

});
.scroll {
  overflow: hidden;
}

.modalDialog:target > body { overflow:hidden; }

.modalDialog {

        overflow:scroll;
        position: fixed;
        top: 0;
        right: 0;
        bottom: 0;
        left: 0;
        background: rgba(255,255,255,1);
        z-index: 99999;
        opacity:0;
        -webkit-transition: opacity 100ms ease-in;
        -moz-transition: opacity 100ms ease-in;
        transition: opacity 100ms ease-in;
        pointer-events: none;
    }

    .modalDialog:target {
        opacity:1;
        pointer-events: auto;
    }

    .modalDialog > .middle {
        width: 80%;
        position: relative;
        margin: 10% auto;
        padding: 10px 20px;
        background: #fff;
    }


    .close {
        background: #fff;
        color: #000;
        line-height: 0px;
        text-align: center;
        position: absolute;
        right: 10px;
        top: 20px;
        width: 24px;
        text-decoration: none;
    }
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<a class="modal" href="#openModal1">Box 1</a>

<div id="openModal1" class="modalDialog">
<a href="#close" title="Close" class="close">x</a>

<div class="middle">
Modal Box 1
This is a sample modal box that can be created using the powers of CSS3.
You could do a lot of things here like have a pop-up ad that shows when your website loads, or create a login/register form for users.
<img src="http://teinesantehnika.ee/content/images/thumbs/0022007_apmale-szklana-silver-375-s_500.jpeg">
</div>
</div>


<a class="modal" href="#openModal2">Box 2</a>

<div id="openModal2" class="modalDialog">
<a href="#close" title="Close" class="close">X</a>
<div class="middle">
Modal Box 2 Box 2 yadda yadda
<img src="http://teinesantehnika.ee/content/images/thumbs/0022007_apmale-szklana-silver-375-s_500.jpeg">
</div>
</div>


<div style="background:#ccc;height:1500px;"></div>

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

Is there a way to incorporate a card logo group into my accordion display?

I am exploring options to include a card logo group alongside my accordion view. My goal is to showcase a card group featuring Visa, MasterCard, and AMEX using SVG. I initially attempted to display 3 images in a row, but I believe there might be a better a ...

Interact with elements using Selenium with dynamic target IDs and AJAX

Currently, I am utilizing selenium for automating various IT administrative tasks. One specific task involves swapping out external drives on a NAS system accessed through an internal webpage. The web interface of the NAS appears to utilize AJAX, which dyn ...

Refreshing a Particular JavaScript File in Electron Upon Request

Lately, I've been working on an Electron app that involves storing data in a javascript file. The data needs to be decrypted when the user logs in and displayed, then encrypted again when the user logs out. While logged in, users can add new data to t ...

When SVGs are loaded, vue-svg-loader is known to eliminate select <g> tags

I have successfully integrated an external SVG into my Vue application as a Vue Component using the vue-svg-loader. This is the loader configuration I used to ensure that IDs are not removed: { test: /\.svg$/, loader: 'vue-svg-loader', ...

Creating a design that resembles boxes for the div elements while ensuring the grid layout remains undisturbed

Hey there! I'm new to using Bootstrap and I'm trying to create boxes that look like the ones on the right side of this page: Unfortunately, I haven't had much luck so far. I tried using padding, but it messed up my grid system. I assigned a ...

Have the input text and submit button aligned on the same line for a sleek

While attempting to create a search box and search submission button, I have tried various methods to align the button and text box on the same line without success. Below is the code snippet I am currently using. return ( <Form onSubmit={submitHa ...

Adjusting the inner div dimensions to be 100% of the body's size without relying on the parent div using JavaScript

I have a main layer with multiple layers stacked on top of it. I need the second layer to extend its width to match the body's width. Main Project: http://example.com What I've tried: I looked into: Solution for matching div width with body ...

Filtering Data with MongoDB Queries

In my data filtering form, I have approximately 15 to 20 dropdown fields. These dropdown fields are meant to provide various options for filtering data. As shown in the image below (from the Zoopla App): https://i.sstatic.net/KOBcc.png The image displays ...

Explore an array of objects to find and retrieve a value from the final matching object in JavaScript

I'm having difficulty retrieving the value for the last event that includes "SearchResults" in the scenario outlined below: Here is a schema of my datalayer where I am looking to gather information. Currently, I have managed to write the following co ...

two clicks on the sc-player

I've encountered a problem of duplicated events when using sc-player.js. I'm trying to change an iframe when a li element is clicked, but the play/pause buttons also trigger the same li click event. Is there a way to control the play/pause funct ...

Unable to invoke method because ViewChild component is undefined

I am currently fetching data from a database and once the data is ready, I want to bind it to a child element using ViewChild in order to pass the object data to the child component. The problem I'm facing is that the component is not immediately "re ...

What causes the entire set of dynamically created cards to collapse when using the toggle collapse button in ngb-bootstrap's Collapse control?

I am currently implementing the ngb-bootstrap collapse feature in my Angular 9 application. Incorporating the ngb-bootstrap collapse functionality within a Bootstrap card, I have multiple cards being generated. Each card is equipped with a collapse button ...

Implementing a comprehensive Node application with a fully stacked npm and package.json structure

If there's a repository with both backend (Node/Express) and frontend client, the structure might look like this: ├── build ├── config ├── coverage │ └── lcov-report ├── dist │ └── static ├── server ( ...

Load the dropdown menu with JSON data but encounter an error: SyntaxError caused by an unexpected token `{` in

I am currently working on populating a dropdown menu with the values of the 'styleName' field from a JSON data file. Here is an example of my JSON data: {"name":{"styleName":"name","fillType":"none","fillTrans":"0","outlineType":"solid","outlin ...

Guide to modifying the value of a JSON attribute in JavaScript

Here's a snippet from my json file: { "@id": "2", "@samsid": "d7058536c89b46c0b58117eff64372f7", "@productname": "Another paid for", "@downloaddate": "2013-05-28 11:37:12Z", "@downloadlocation": "2AFoGZVJpFHzspQD9UoE2C4VFYc8idiO4ebaRx4uEvtG+ ...

Tips for personalizing your Magento 2 theme

As someone new to Magento 2 and front-end development with a basic knowledge of HTML and CSS, I am eager to customize the blank theme in Magento 2 to gain a deeper understanding of how things work. However, despite reading through the documentation, I am u ...

What is the best way to ensure the website title is displayed properly on a mobile browser?

Concern I have been encountering difficulties while constructing a portfolio website using bootstrap 5. The issue arises with the navbar, causing the Title to overlap the hamburger menu and extend beyond the edge of the screen. Strangely enough, this prob ...

Diving into Angular2 template forms: unraveling the mysteries of the reset function

After going through the template forms tutorial in Angular2, I'm facing a bit of confusion regarding the behavior of the native reset JavaScript function on Angular2 ngModel. While it's not explicitly clarified in the official documentation, my u ...

There seems to be a glitch in the functionality of annotations when using annotator.js

Currently, I am utilizing annotator.js to store the range in mysql. The following code fragment is being used for highlighting text within my file: <script src="/js/pdfjs/annotator.js"></script> <script> $(function(){ var annotation ...

Issue with localStorage failing to save comments added at the beginning

Here is a link to the fiddle I am working on. My goal is to store prepended comments in a ul by saving the ul as a variable, and use localStorage to save it in the browser. The HTML structure of the project: <h1>Commentia!!!</h1> <textarea ...