Is there a way to create a blurred background effect while the popup is in use?

I prefer my popup to have a dark or blurred background when active. The popup itself should remain the same, with only the background being darkened. I would like a dark layer overlaying the entire page. This script should be compatible with any website where it is included.

The code must be written in Javascript. When the popup is closed, the page should return to its normal state as per the provided code.

window.onload = addElement;

function addElement() {
  // create a new div element 
  // and give it popup content 
  var newDiv = document.createElement("div");
  var texts = 'erd';
  newDiv.innerHTML += '<div id="popup" style=" position: absolute;top: 5%;width: 800px;height: 200px;margin: auto;z-index: 99999;display: block;left:25%;background-color: #fff;  border: 1px solid #ddd;  border-radius: 5px;  box-shadow: 0 1px 6px 4px #000;  overflow: hidden;   padding: 10px;"><div class="popup_body" style="  height: 160px;">' + texts + '</div><button style="padding: 10px;" class="close_button"onClick="closePopup()">Close</button><button  style="padding: 10px;" class="close_button"onClick="showInfo()">More Info</button></div>';

  // add the newly created element and its content into the DOM 
  var currentDiv = document.getElementById("main_container");
  document.body.insertBefore(newDiv, currentDiv);

  // open popup onload
  openPopup();
}

function openPopup() {
  var el = document.getElementById('popup');
  el.style.display = 'block';
  var BG = document.createElement("div");
  BG.style.background-color = 'black'; // Change this line
  BG.style.width = '100%';
  BG.style.height = '100%';
}

function showInfo() {
  window.location.href = '../testing/info.php';
}

function closePopup() {
  var el = document.getElementById('popup');
  el.style.display = 'none';
}
teefsffstfssgrhhsggsrhservgssfvrhthtrdg rthsgssdsgsssssgegdgssstyygghdeeffsdfsfsdgtrhhfhtrrthsgssdsgsnjissgegdgss
sdrfdsdeeffsdfsfsdgtrhhfhtrrthsgssd

sgsgfksgegdgssjhsssdeeffsdfsfsdgtrhhfhtrrthsgssdsgsstrsgegdgssuiopssdeeffsdfsfsdgtrhhfhtrrthsgssdsgsesrrressgegdgssslkkjsdeeffsdfsfsdgtrhhfhtrvvvtkiyoyuirt

Answer №1

Here is the solution:

window.onload = addElement;

function addElement() {
  // create a new div element 
  // and populate it with popup content 
  var newDiv = document.createElement("div");
  var texts = 'erd';
  newDiv.innerHTML += '<div id="popup" style=" position: absolute;top: 5%;width: 800px;height: 200px;margin: auto;z-index: 99999;display: block;left:25%;background-color: #fff;  border: 1px solid #ddd;  border-radius: 5px;  box-shadow: 0 1px 6px 4px #000;  overflow: hidden;   padding: 10px;"><div class="popup_body" style="  height: 160px;">' + texts + '</div><button style="padding: 10px;" class="close_button"onClick="closePopup()">Close</button><button  style="padding: 10px;" class="close_button"onClick="moreInfo()">More Information</button></div>';

 // Add The Background cover
  var BG = document.createElement("div");
  BG.style.width = '100%';
  BG.style.height = '100%';
  BG.style.background = 'black';
  BG.style.position = 'fixed';
  BG.style.top = '0';
  BG.style.left = '0';
  BG.style.opacity = '0.9';
  BG.style.display = 'none';
  BG.setAttribute("id", "bgcover");
  
  // append the newly created elements and its content into the DOM 
document.body.appendChild(BG);
document.body.insertBefore(newDiv, BG);
  // open popup onload
  openPopup();
}

function openPopup() {
  var el = document.getElementById('popup');
  var BG = document.getElementById('bgcover');
  el.style.display = 'block';
  BG.style.display = 'block'; 
}

function moreInfo() {
window.location.href = '../testing/storing.php'; 
}

function closePopup() {
  var el = document.getElementById('popup');
  var BG = document.getElementById('bgcover');
  el.style.display = 'none';
  BG.style.display = 'none';
}
<body>
  lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus convallis condimentum interdum. Aliquam ultricies risus ac lacus malesuada tristique. Sed vitae velit vel orci tincidunt eleifend ut et quam. Etiam imperdiet justo eget nulla efficitur, eu pretium sem aliquet. Fusce nec blandit ex. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Ut eu viverra mauris.
</body>

Answer №2

To reduce the background opacity, it should only be done when the popup is in use.

Adjusting the background color to rgba(0,0,0,0.4);

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 Chrome Extension XHR always gets a 403 response except when it is triggered from the file:/// protocol

My current project involves the development of a Chrome Extension that relies on the fixer.io API for accessing currency exchange rates. To test this extension, I typically use a simple HTML page where I embed my JavaScript using <script> tags. When ...

Unique twist on the Bootstrap grid: perfectly centered

I'd like to design a 12-column Bootstrap grid with specific colors. The header should be light blue, the body in green, and the footer orange against a grey background. I want the grid to be centered on the screen with horizontal light blue stripes m ...

Transferring user inputs to the server through jQuery-AJAX

I've encountered some issues when trying to send form data inputs to the server. Despite alerting each form input, I keep getting empty alerts. Here's my code snippet: With my current code, it only alerts 0. However, posting normally seems to wor ...

jQuery plugin that controls scrolling speed using the mousewheel

I am trying to replicate the header design of Google+ which includes a search bar that moves when scrolling. Specifically, when the user scrolls down, the search bar shifts to top:-60px and the second horizontal menu shifts from top:60px to top:0 becoming ...

Swap out a component for another using a higher order component (HOC perhaps?)

I have noticed that certain libraries like "framer-motion" in react utilize this syntax, for instance to include an animated H1: <motion.h1> where the h1 tag is enhanced with animations specified in the component's props. What confuses me is ho ...

Evaluate a program to identify prime numbers

I am currently working on a JavaScript program that utilizes recursion to determine whether an input is a prime number or not. Within my code, I've defined the isPrime function. The base cases are set to return false when x==1 and true for x==2, cons ...

Show a modal when the axios request is finished

EDIT: After some exploration, I've shared the solution I found in case it helps others facing a similar issue. I'm currently working on building a reusable Bootstrap 5 modal child component within Vue3. The goal is to pass an ID as a prop from t ...

Steps for returning a res.send(req.body) upon sending back to a function

I'm currently working on sending the req.body from a POST request route back to the executing function for further processing. The structure of req.body is as follows: { username: 'aa', password: 'ss' } After making the post requ ...

Utilizing functions as parameters and implementing a flexible number of arguments

I have two different functions: Function called 'first' which takes 1 argument Another function called 'second' that takes 2 arguments Next, there is a third function that accepts a function and a value as its parameters. I am strugg ...

purging data from javascript objects

In my Node.js HTTP server, I am using 'connect' to build a web service that currently parses JSON requests into an Object, performs operations, and returns a synchronous response. The JSON data comes from an ecommerce cart and results in an Objec ...

Creating dynamic stripes on MUI LinearProgress for lively animations

I'm looking to add animation to MUI LinearProgress. I have the animation keyframes code, but I'm not sure how to implement the stripes effect. Any advice would be appreciated. Here is the code snippet I have: import React, { FunctionComponent } ...

nested dropdowns in Bootstrap 4

I'm currently working on a nested dropdown menu feature. Although I have successfully implemented the functionality to display the next level, I am facing an issue where it does not close upon clicking. Check out my code here! Here is the JavaScript ...

Why React.js and webpack are restricting the use of var, let, const?

I'm facing a puzzling issue that I just can't seem to solve. Let me show you a snippet from my Graph.js file: class Graph extends React.Component { @observable newTodoTitle = ""; s = 40 The error in webpack is showing up like this: E ...

Aligning a bootstrap button group at the center of a container

Looking for suggestions on how to center a Bootstrap button group (btn-group) within an element? For instance, consider the following structure: <div id='toolbar'> <div class="btn-group"> <button type="button" class="b ...

What are the steps to resolve a Fetch request issue with a Node.js server?

I am attempting to make a simple POST request using the fetch method. I am working on building a contact form using Vanilla Javascript, HTML, and CSS on the front end, while utilizing Node.js / Express on the backend. Take a look at my front end code: ...

What causes parseInt to transform a value into infinity?

Here is what I'm working on: let s = '50'; let a = parseInt(s); console.log(a); //outputs 50 console.log(_.isFinite(a)); //outputs false I'm curious why parseInt turns 'a' into infinity when 'a' is set to 50? ...

Asynchronous NestJs HTTP service request

Is there a way to implement Async/Await on the HttpService in NestJs? The code snippet below does not seem to be functioning as expected: async create(data) { return await this.httpService.post(url, data); } ...

Tips for seamlessly integrating an overlay into a different image

My current system is set up to overlay the image when you check the checkboxes. However, I'm facing an issue with positioning the image inside the computer screen so it's not right at the edge. Can anyone provide assistance with this? <html&g ...

Tips for creating a pop-up window on an ASP.NET web form

I am looking to incorporate a popup window as a child of my primary asp.NET form. The popup window will allow users to input information using a dropdown list. When the popup window appears, it will take focus and disable the main window. ...

Every attempt to connect with the webservice via an ajax call has ended in failure

I am encountering an issue with a webservice call that I am making using jQuery's AJAX method. Despite receiving JSON data from the webservice when accessed directly through the browser, my site always triggers the error function rather than the succe ...