What could be the reason my black overlay doesn't show up when the button is clicked?

Attempting to craft a pop-up window on my own, I encountered an issue. Upon pressing the button, instead of the anticipated pop-up appearing and darkening the background below it, the entire page freezes with no sign of the pop-up box.

If I eliminate the div responsible for darkening the background, the pop-up functions as expected.

Below is the HTML code containing script tags:


      let visible = false;
      
      $('#showBox').on('click', function(params) {
        if (visible) {
          $('.box').removeClass('boxVisible');
          $('.blackenBackground').removeClass('boxVisible');
          visible = false;
        } else {
          $('.box').addClass('boxVisible');
          $('.blackenBackground').addClass('boxVisible');
          visible = true;
        }
      })
    

      .box {
        background: pink;
        height: 500px;
        width: 500px;
        position: absolute;
        top: 50%;
        left: 50%;
        z-index: 3;
        transform: translate(-50%, -50%);
        border-radius: 1%;
        opacity: 0;
        transition: 0.4s;
        transition-timing-function: ease-out;
      }

      .boxVisible {
        opacity: 1;
      }

      .blackenBackground {
        position: fixed;
        top: 0;
        left: 0;
        height: 100vh;
        width: 100vw;
        background: black;
        opacity: 0;
        z-index: 2;
      }
    

      <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
      <div class="box"></div>
      <p>Some lorem text.....</p>

      <button id="showBox">Show Box</button> 

      <!-- When I remove this popup box works perfectly but background isn't darkening and my page hangs -->
      
      <div class="blackenBackground"></div>
    

Answer №1

Your fixed position div is causing issues with mouse events. Even though the opacity is set to 0, the box remains technically visible, blocking clicks on other elements.

To resolve this problem, ensure that the box is completely hidden so that it does not interfere with user interaction.

let isVisible = false;
$('#showBox').on('click', function (params) {
    if(isVisible){
        $('.box').removeClass('boxVisible');
        $('.blackenBackground').removeClass('boxVisible');
        isVisible = false;
    }else{
        $('.box').addClass('boxVisible');
        $('.blackenBackground').addClass('boxVisible');
        isVisible = true;
    }
})
.box{
    background: pink;
    height: 500px;
    width: 500px;
    position: absolute;
    top: 50%;
    left: 50%;
    z-index: 3;
    transform: translate(-50%, -50%);
    border-radius: 1%;
    opacity: 0;
    transition: 0.4s;
    transition-timing-function: ease-out;
}

.boxVisible{
    opacity: 1;
    display: block;
}

.blackenBackground{
    position: fixed;
    top: 0;
    left: 0;
    height: 100vh;
    width: 100vw;
    background: black;
    opacity: 0;
    display: none;
    z-index: 2;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="box"></div>
<p>Lorem ipsum dolor sit amet...</p>
<button id="showBox">Show Box</button>
<div class="blackenBackground"></div>

Answer №2

Would you like something similar to this? I have included an id attribute in the div tag and modified your addClass function in jQuery to

document.getElementById('dimmer').style.display= 'none / block'
within your if-else statements. Additionally, I have adjusted the .css class to:

.blackenBackground{
    pointer-events: none;
    background:#000;
    opacity:0.5;
    position:fixed;
    top:0;
    left:0;
    width:100%;
    height:100%;
    display:none;
}

Here is the link to the code on jsFiddle: https://jsfiddle.net/j0d8emsc/

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

Display text when hovered over or clicked to insert into an HTML table

I have an HTML table connected with a component field gameArray and I need it to: Show 'H' when the user's cursor hovers over TD (:hover) and the corresponding field in gameArray is an empty string, Fill the gameArray field after a click. ...

What is the best way to change the size of a QR code

My current HTML code: <input id="text" type="text"/> <div id="qrcode"></div> Previous version of JAVASCRIPT code: var qrcode = new QRCode("qrcode"); $("#text").on("keyup", function () { qrcode.makeCode($(this).val()); }).keyup().focus ...

What is the inner workings of CSS?

I'm curious about the inner workings of CSS. Does the HTML get interpreted before CSS, or vice versa? Or does it all apply as soon as the browser has constructed the DOM? I would appreciate a detailed explanation on this topic. ...

Replace all empty space in the current line with dots. This is a multi-line text

Trying to achieve a specific look for an html page, which involves filling whitespace with dots until the end of the line without using javascript. Various solutions have been attempted for single-line elements, but none have worked for multi-line element ...

Trouble with incrementing and decrementing the product quantity in the shopping cart using jQuery on the same Submit button (PHP)

While working on adjusting cart product quantities using jQuery and form submission with button clicks, I encountered some issues. The first cart product works correctly, but the others do not due to an ID problem. Please refer to my code below: $(docum ...

Avoid showing an image when it is outside the div container

Within a single div, I have an assortment of images that are dynamically repositioned using JQuery. $("#"+carElem).css({"left": pos.left+50 +"px"}); I am currently utilizing absolute positioning (although relative positioning yields the same outcome). Is ...

Learn the process of connecting checkboxes to a database in MeanStack using Angular

Hey everyone, I'm currently working with AngularJS ng-repeat and I am trying to dynamically bind a checkbox based on a true or false value from the database. However, even when the database value is set to true, the checkbox does not automatically che ...

Is there a way to keep a div element stationary during scrolling without using fixed positioning?

Is there a way to prevent a div from moving when scrolling up or down without using the position:fixed property? When an element is fixed, the scroll bar disappears making it impossible to reach the element by scrolling. div{ position:fixed; top:1 ...

Action creator incomplete when route changes

In my React-Redux application, there is an action creator that needs to make 4 server calls. The first three calls are asynchronous and the fourth call depends on the response of the third call. However, if a user changes the route before the response of t ...

Struggling to grasp the concept of using z-index in CSS

As a programmer versed in Python, C, Java, and Delphi, I am not a web developer or designer by trade. However, when required to fill those roles, I do my best; please be patient with me. :-) I have created a map (with a background image as a div), where I ...

Having trouble installing npm package in Angular project

After cloning my project from GitLab, I encountered an issue while trying to install the NPM packages. When I ran npm install, an error popped up: https://i.stack.imgur.com/WNT5s.png Upon checking the log file, I found the following error message: 3060 ...

Preventing horizontal swiping while vertically scrolling on mobile devices

How can I prevent horizontal swipe functionality from interfering with vertical scrolling on a webpage? I have successfully blocked vertical scrolling but need help finding a solution for preventing horizontal swiping. Has anyone else encountered this issu ...

Modify the scoped variable using Angular's $resource module

I am facing an issue with my Angular application where I cannot get the results of a second $resource call to update a scoped variable. Initially, I am able to generate the initial view from an Angular $resource call to an external API and display the data ...

Unable to retrieve data from the $.getJSON method

Using $.getJSON in jQuery, I am retrieving the necessary data from the server. Below is an example of how it is structured: $.getJSON("/dataParser/parseVoltage",function(jsondata, status) { if (status == "error") { console.log("Error occurred whil ...

What is the best way to extract value from a JSON object with jQuery?

How can I retrieve the value of 'FRI' from the JSON feed returned by an AJAX call using jQuery? $.ajax({ url: query, type: "GET", dataType: "json" success: function(data) { var day = // extract data value from JSON ...

Adjust the width of the scrollbar for the <aside> section

<aside class="sidenav navbar navbar-vertical navbar-expand-xs border-0 border-radius-xl my-3 fixed-left ms-3" id="sidenav-main"></aside> Within this aside element, I have implemented a scrollbar. However, I am seeking to adjust ...

The strategic placement of Datatables within the webpage's structure

Are there any additional resources available for learning about DOM positioning besides the one mentioned here? Specifically, I am trying to position the entries select field and entries info below the pagination. How can this be accomplished? This is my ...

`Error with Bootstrap breakpoints in mobile display`

While working on a responsive login layout using Bootstrap 5, I encountered a strange issue. The design looked fine in the web view, but when switching to mobile view, the md and sm breakpoints didn't trigger as expected. However, after pasting the co ...

How to make divs occupy the entire space of a parent div?

I've been scouring the website for a solution, but nothing I've found has been helpful. I'm attempting to create a grid within a containing div where a default (and eventually user-specified) number of divs will fill up the space. I'm t ...

Experiencing an unusual issue with grunt: The Gruntfile.js seems to be missing the 'flatten' method

Encountering an unusual error message while attempting to run grunt stated: TypeError: Object Gruntfile.js has no method 'flatten' Being a beginner with node.js, npm, and grunt, I believe my installation of node, npm, and grunt was done correctl ...