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>