javascript - 反应:出现 Material-UI 对话框时模糊背景
解决方案
解决此问题的一种方法是在对话框背景上使用背景过滤器,它可以模糊背景。backgroundFilter css 属性是一个相对较新的 css 功能,但它在 Chrome、Edge、Samsung Internet 和 Safari 上运行良好。目前它不适用于 Firefox。但也许这适合你?这是一个代码示例:
import React from "react";
import { Dialog, DialogContent, makeStyles, Theme, Typography } from "@material-ui/core";
const useStyles = makeStyles((theme: Theme) => ({
backDrop: {
backdropFilter: "blur(3px)",
backgroundColor:'rgba(0,0,30,0.4)'
},
}));
export default function ExampleDialogComponent() {
const classes = useStyles();
return (
<Dialog
open={true}
BackdropProps={{
classes: {
root: classes.backDrop,
},
}}
onClose={() => {}}
>
<DialogContent style={{ textAlign: "center" }}>
<Typography variant="body1">Example Dialog</Typography>
</DialogContent>
</Dialog>
);
}
材质 UI 5 的更新
在即将发布的Material UI 5版本中,您可以使用如下样式的组件:
import Box from "@material-ui/core/Box";
import { experimentalStyled as styled } from "@material-ui/core/styles";
import Dialog, { DialogProps } from "@material-ui/core/Dialog";
import DialogTitle from "@material-ui/core/DialogTitle";
import DialogContent from "@material-ui/core/DialogContent";
import DialogActions from "@material-ui/core/DialogActions";
import Button from "@material-ui/core/Button";
import CssBaseline from "@material-ui/core/CssBaseline";
const BlurryDialog = styled(Dialog)<DialogProps>(({ theme }) => ({
backdropFilter: "blur(5px)",
// other styles here...
}));
export default function ExampleNextDialog() {
return (
<>
<CssBaseline />
<BlurryDialog fullWidth onClose={() => {}} open={true} maxWidth="xs">
<DialogTitle>Example Dialog</DialogTitle>
<DialogContent>Example Content Here</DialogContent>
<DialogActions>
<Button>ooooh.</Button>
</DialogActions>
</BlurryDialog>
<Box
sx={{
minHeight: "100vh",
background:
"url(https://source.unsplash.com/random) no-repeat center center",
backgroundSize: "cover",
}}
></Box>
</>
);
}
或者像这样使用 sx 道具:
import Box from "@material-ui/core/Box";
import Dialog from "@material-ui/core/Dialog";
import DialogTitle from "@material-ui/core/DialogTitle";
import DialogContent from "@material-ui/core/DialogContent";
import DialogActions from "@material-ui/core/DialogActions";
import Button from "@material-ui/core/Button";
import CssBaseline from "@material-ui/core/CssBaseline";
export default function ExampleNextDialog() {
return (
<>
<CssBaseline />
<Dialog
fullWidth
onClose={() => {}}
open={true}
maxWidth="xs"
sx={{
backdropFilter: "blur(5px)",
//other styles here
}}
>
<DialogTitle>Example Dialog</DialogTitle>
<DialogContent>Example Content Here</DialogContent>
<DialogActions>
<Button>ooooh.</Button>
</DialogActions>
</Dialog>
<Box
sx={{
minHeight: "100vh",
background:
"url(https://source.unsplash.com/random) no-repeat center center",
backgroundSize: "cover",
}}
></Box>
</>
);
}
推荐阅读
- node.js - 如何在快速路由中使用 mysql2/promise?
- puppeteer-sharp - 对于非常大的网页,使用 PuppeteerSharp 生成 PDF 时出现“算术溢出”异常
- python - Selenium ElementNotInteractableException 与 ASP.NET 报告查看器 (Python)
- database - Mongo跨集合、嵌套文档的分页查询
- c++ - 如何创建一个二维数组并计算它的平均值?
- android - Flutter Jitsi 在释放模式下停止
- reactjs - 基于右键单击在 React 中条件加载组件
- reactjs - 如何有条件地更改 React Native 中平面列表上的数据?
- next.js - 重定向来自 NextJS 子域的所有流量
- javascript - how to create loading spinner react