最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Blurring background when React portal is displayed - Stack Overflow

programmeradmin8浏览0评论

I'm using React portals to create a modal popup after form submission. I would like to blur just the background and to show the modal like in picture 2. I used document.body.style.filter = "blur(5px) in the first picture but it blurs everything. CSS for the modal and modal root (my code is almost identical to the React docs for portals)

```
#modal-root {
  position: relative;
  z-index: 999;
}
.modal {
  background-color: rgba(0,0,0,0.5);
  position: fixed;
  height: 100%;
  width: 100%;
  top: 0;
  left: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: auto;
  z-index: 99999;
}```

I'm using React portals to create a modal popup after form submission. I would like to blur just the background and to show the modal like in picture 2. I used document.body.style.filter = "blur(5px) in the first picture but it blurs everything. CSS for the modal and modal root (my code is almost identical to the React docs for portals)

```
#modal-root {
  position: relative;
  z-index: 999;
}
.modal {
  background-color: rgba(0,0,0,0.5);
  position: fixed;
  height: 100%;
  width: 100%;
  top: 0;
  left: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: auto;
  z-index: 99999;
}```

Share Improve this question asked Jun 7, 2018 at 18:36 wildairwildair 6372 gold badges6 silver badges12 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 9

Every child of the element that you blur will have that filter applied. Since you have similar code to the portal docs then im going to assume your html structure is something like this

<html>
  <body>
    <div id="app-root"></div>
    <div id="modal-root"></div>
  </body>
</html>

if this is the case then apply the filter to app-root aka...

document.getElementById('app-root').style.filter = 'blur(5px)'

Because your modal has fixed position, when your modal is open, to blur the background, add

  backdrop-filter: blur(8px);

or

  backdropFilter: blur(8px);

to the style of your modal body and that should do the job.

Can you wrap the body in an outter div, then blur that making sure your modal is outside your "blur" div?

发布评论

评论列表(0)

  1. 暂无评论