问题描述
我有一个简单的Material UI对话框,其中包含一个网格,并且它的滚动条可以滚动几个像素,即使屏幕足够大,可以容纳整个对象.
I have a simple Material UI dialog containing a grid, and it has a scrollbar that can scroll a few pixels even though the screen is big enough to contain the whole thing.
<Dialog open={isOpen} onClose={() => changeIsOpen(false)}>
<DialogContent>
<Grid container spacing={3}>
<Grid item xs={12} sm={6}>
<TextField label="First Name" fullWidth />
</Grid>
<Grid item xs={12} sm={6}>
<TextField label="Last Name" fullWidth />
</Grid>
<Grid item xs={12}>
<TextField label="Username" fullWidth />
</Grid>
</Grid>
</DialogContent>
<DialogActions>
<Button
color="secondary"
variant="contained"
onClick={() => changeIsOpen(false)}
>
Cancel
</Button>
<Button
color="primary"
variant="contained"
onClick={() => changeIsOpen(false)}
>
OK
</Button>
</DialogActions>
</Dialog>
此代码位于 https://codesandbox.io/s/cool-cherry-or0r8供您查看.
我不想使用 overflow:hidden
,因为如果页面太小,将会有一个滚动条,这是正确的.(在这个具有3个字段的玩具示例中,这种情况不太可能发生,但在较大的对话框中很容易发生).
I don't want to use overflow: hidden
, because if the page is too small, there will be a scrollbar and that's correct. (Not likely to happen in this toy example with 3 fields, but easily possible in larger dialogs).
我认为问题与flexbox和< Grid>
使用的负边距之间的交互作用有关,但我无法完全解决.
I think the problem has something to do with interactions between flexbox and the negative margins that <Grid>
uses, but I can't quite work it out.
推荐答案
更新:
DialogContent似乎是罪魁祸首,我们可以简单地尝试用下面给出的< div/>
替换< DialogContent/>
<div style={{ padding: 20, overflow: "scroll" }}>
<Grid container spacing={3}>
<Grid item xs={12} sm={6}>
<TextField label="First Name" fullWidth />
</Grid>
<Grid item xs={12} sm={6}>
<TextField label="Last Name" fullWidth />
</Grid>
<Grid item xs={12}>
<TextField label="Username" fullWidth />
</Grid>
</Grid>
</div>;
忽略此解决方案:
DISREGARD THIS SOLUTION:
将 DialogContent
替换为以下内容:
<DialogContent>
<div style={{ overflow: "hidden", height: "100%", width: "100%" }}>
<div
style={{
paddingRight: 17,
height: "100%",
width: "100%",
boxSizing: "content-box",
overflow: "scroll"
}}
>
<Grid container spacing={3}>
<Grid item xs={12} sm={6}>
<TextField label="First Name" fullWidth />
</Grid>
<Grid item xs={12} sm={6}>
<TextField label="Last Name" fullWidth />
</Grid>
<Grid item xs={12}>
<TextField label="Username" fullWidth />
</Grid>
</Grid>
</div>
</div>
</DialogContent>
演示: https://codesandbox.io/s/09ng6
为此答案提供信用: https://stackoverflow.com/a/16671476/7427111
这篇关于简单材质UI对话框示例具有不需要的滚动条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!