Preventing Continuous Scrolling When a Popup Appears

I am struggling with keeping the scrolling disabled when a popup div is visible in the center of the screen. I have attempted to use an overlay in the browser, but it doesn't seem to be working. Any suggestions on how to achieve this using jQuery and CSS?

Here is the code snippet:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Simple Jquery popup</title>
<script type="text/javascript" src="jquery-1.9.1.min.js"></script>
<style type="text/css">
 #popup
            {
    display: none;
    width: 640px;
    height: 480px;
    overflow: auto;
    position: absolute;
    z-index: 2000;
    top: 50%;
    left: 50%;
    margin-left: -320px;
    margin-top: -240px;
    border: thin dashed #8f44ad;
    padding-bottom: 20px;
    background-color: #2d3e50;
    font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
    font-size: 24px;
    line-height: 35px;
    letter-spacing: 2px;
    color: #FFFFFF;
            }
  .close
{
    float: right;
    color: #2a80b9;
    cursor: pointer;
}
  #overlay
            {
                display: none;
                width: 100%;
                height: 100%;
                left: 0;
                top: 0;
                position: fixed;
                z-index: 1000;
                background: #96a6a6;

            }
#style {
    background-color: #2d3e50;
    font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
    font-size: 18px;
    line-height: 18px;
    color: #FFF;
}
#main {
    width: 600px;
    height: 150px;
    margin-top: 200px;
    margin-left: 250px;
    margin-right: auto;
    padding: 30px;
    border: thin dashed #FFF;
    font-size: 25px;
    color: #fff;
    line-height: 35px;
    letter-spacing: 2px;
    padding-left: 250px;
}
#main input
{
    width: 400px;
    height: 50px;
    border: 1px solid #FFF;
    color: #FFF;
    font-size: 20px;
    line-height: 35px;
    letter-spacing: 2px;
    background-color: #16a086;
    padding: 0px;
    margin-left: 20px;
}
#main input:hover
{
    background-color:#27ae61;
}
</style>
</head>
<body id="style">
<div id="popup">
<span class="close">&times;</span>
<br />
<br />
<div style=" width:600px float:left" align="center ">
Click on the button above to close this box
</div>
</div>
<div id="overlay"></div>
<div id="main">
<div style="width:600px;float:left">
<span>This is the basic view of the page</span>
</div>
<br />
<br />
<div style="width:600px;float:left">
<input type="submit" value="Click Here To view the popup" id="showpopup" />
</div>
</div>
<script type="text/ecmascript">
            $(document).ready(function(){
                            $("input#showpopup").click(function(){
                                $("div#overlay").fadeIn('500');       
                                $("div#popup").delay('800');
                                $("div#popup").fadeIn('500');         
                           }); 

    $(document).on('click', '.close', function(){
                                $("div#popup").fadeOut('500');      
                                $("div#overlay").delay('500');
                                $("div#overlay").fadeOut('500');
                           });
                });

        </script>
</body>
</html>

Answer №1

Upon opening the popup, adjust the overflow attribute in the CSS to hidden as follows:

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

Once you close the popup, revert back to the default setting:

$('body').css('overflow','auto')

Full Code:

 $(document).ready(function () {
     $("input#showpopup").click(function () {
         $("div#overlay").fadeIn('500');
         $("div#popup").delay('800');
         $("div#popup").fadeIn('500');
         $('body').css('overflow', 'hidden');  //Include this line
     });

     $(document).on('click', '.close', function () {
         $("div#popup").fadeOut('500');
         $("div#overlay").delay('500');
         $("div#overlay").fadeOut('500');
         $('body').css('overflow', 'auto');  //Include this line
     });
 });

fiddle

Answer №2

It appears that your request is to prevent full page scrolling, specifically for the body.

To achieve this, you can apply the CSS attribute overflow:hidden to the <body> tag when the popup is active, and switch it back to auto when the popup is inactive.

Answer №3

To prevent overflow, include the following CSS for the body element:

$('#show_my_popup').click(function(){
    $('body').css('overflow', 'hidden');
});

Remember to remove the overflow once the popup is closed:

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

If you want to avoid the page jumping to the right when a scrollbar appears, you can implement this solution:

$('#show_my_popup').click(function(){
    // Add right margin to prevent shifting
    $('body').css('margin-right', (window.innerWidth - $('body').width()) + 'px');
    $('body').css('overflow', 'hidden');
});

And don't forget to clear the styles when closing the popup:

...
$('body').css('overflow', '');
$('body').css('margin-right', '');

Answer №4

Apply the CSS property overflow:hidden to the body element when displaying a dialog.

Answer №5

Utilizing jQuery for archiving purposes. Issue with overflow in IOS devices addressed below with code snippets for enabling and disabling background scroll.

  1. Stop touchmove on Body.

    $(document).bind('touchmove', function(e){
                        e.preventDefault(); 
                        // alert('prevent scroll');         
                    });
    
  2. Allow scrolling in Popup container.

    $('.mfp-auto-cursor .mfp-content').bind('touchmove', function(e){
                        e.stopPropagation();
                        // alert('allow scroll');
                    });
    
  3. Re-enable body scroll after closing the popup.

    $(document).unbind();
    

Answer №6

Would it be possible to use the position fixed property on the popup container and set a higher z-index than other content? This would keep the scrolling functionality intact while ensuring that the popup appears on top of the page.

.popup-container {
    position: fixed;
    z-index: 10;
}

Thanks, J.

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

Can text be both vertically and horizontally aligned in a div at the center?

By using only one tag and the corresponding CSS, we can achieve center alignment. Take a look at this example: http://jsfiddle.net/EqTsu/ <style> #test{ width: 100px; height: 100px; text-align: center; border: solid 1px #f ...

jQuery Slider Tutorial: Step-by-step guide to creating a unique slider

I am looking for a way to smoothly transition through a series of images when the mouse cursor reaches the middle of the first image. The idea is that when the mouse cursor reaches the middle of the first image and begins to scroll, the images will then sl ...

What steps can I take to ensure my header appears perfectly aligned? (HTML/CSS)

My header on my website is all messed up with the links appearing incorrectly. I suspect it's an issue with style.css, so I attempted to modify the style.css file but had no luck. I have very limited experience with .css and cannot seem to figure out ...

Is there a way to delete content in HTML after a particular text using Python and BeautifulSoup4?

I am currently in the process of scraping information from Wikipedia. My goal is to extract only the necessary data and filter out any irrelevant content such as See also, References, etc. <h2> <span class="mw-headline" id="See ...

Is it possible to adjust a CSS value once a JS value hits a specific threshold?

Currently, I am developing a web game where the website displays different emotions, including anger. I have successfully implemented the anger level internal coding and I am now focusing on adding a color-changing feature when the anger level reaches a sp ...

Load form elements using jQuery by triggering an onchange event on a drop down box via ajax

When selecting an option from the dropdown menu, I need to dynamically load form elements based on a statement <select name="type" id="type"> <option value="1">input</option> <option value="2">text</option> </select& ...

Visual Studio 2008 mandates the use of XHTML

I prefer using HTML over XHTML, but for some reason VS2008 is forcing me to use it. Whenever I type < br in an ASPX file, it automatically changes to < br /> Normal HTML4.01 does not have these auto-completions. Is there a way to disable this a ...

Is it possible to add a CSS filter to a background image?

How can I achieve a background image with blur effect using CSS? body{ background-color: transparent !important; background-image: url("img.jpg"); background-size: cover; background-position: center center; background-repeat: no-repea ...

Is there a way to remove the entire row if I dismiss a bootstrap alert?

Hey there! I'm having a little issue with an alert in a row-fluid. Curious to see? Check out my fiddle. So, when I click the "x" button to dismiss the alert, the row is still visible. Any idea how I can make the entire row disappear when I dismiss it? ...

When using requestdispatcher.forward to load a page in JSP, jQuery may encounter issues and fail to work properly

Currently, I am tackling servlets and have come across an issue. I have a jsp page that contains some jQuery scripts. When the page is loaded from the servlet using response.sendRedirect(), all statements utilizing jQuery work flawlessly as showcased below ...

Conceal one object when the other is revealed?

Is there a way to hide the element with the class .close-button while showing another element with the ID #loading-animation? Can jQuery conditionals help achieve this? For example: if ($('#loading-animation').is(':visible')) { $ ...

Converting HTML Forms to Arrays

Hey there, I've been working on a pizza ordering program lately. My approach involved creating a form with different inputs and then attempting to collect that information into an array. Unfortunately, things didn't quite work out as expected, an ...

Discover the following `<td>` identifier through the act of clicking on a separate `<td>` element

I have a few td's with specific ids: <td id="first">first</td> <td id="second">second</td> <td id="third">third</td> <td id="fourth">fourth</td> Whenever I click on one of the td elements, I want to re ...

The error message "POST .../wp-admin/admin-ajax.php net::ERR_CONNECTION_CLOSED" appears when a WordPress AJAX request receives data that exceeds 1MB in size

When I attempt to submit a jquery ajax form from the frontend and upload a blob (a variable of string) as a .txt file to WordPress using wp_handle_upload(), everything works smoothly until the file size reaches around 1mb. At that point, an error is displa ...

When you apply margins in CSS and HTML, it can cause elements to break out of the row alignment

I'm struggling with my html code that contains elements arranged in rows: <div class = "row"> <div class="col-sm-3 cont-box"> <h1>Title1<h1> </div> <div class="col-sm9 cont-box"> <img src="onepic.jpeg" class=" ...

We are currently experiencing issues with Internet Explorer retrieving data from input type text

My HTML code includes input elements with type="text" like this: <input type="text" class="f_taskname" value=""/> Once the user enters text and hits enter, the following script is triggered: var task_name=$('#filter_body').find('.f_ ...

Is it possible to swap out images using srcset attribute?

Currently, I am facing an issue regarding changing the img on mobile and tablet devices to display different images. As a beginner with jQuery, I couldn't resolve it using that framework, so I am exploring options with html5. I am wondering if there ...

Customizing CSS Styles in ASP.NET MVC for Multilingual Websites

We are looking to make our ASP-MVC website multicilingual and support different languages. One challenge I am facing is how to apply distinct style-sheets for each language, especially when dealing with right-to-left (RTL) languages like Hebrew or Arabic c ...

Using CSS Grid to Align Images

I want to utilize CSS 3 Grid to position specific images on my home page that I imported. The desired outcome is as follows: https://i.sstatic.net/aYshN.jpg Currently, my grid looks like this: https://i.sstatic.net/oEUWX.png Below is the code snippet: ...

The CSS infinite animation becomes erratic and sluggish after a short period of time

My website featured a banner with a series of thumbnail images that animated at different intervals and looped indefinitely. Initially, the animations ran smoothly, but after a few seconds, they began to slow down and lose consistency. To troubleshoot, I u ...