css 如何使材料UI模态和对话框可滚动?

jgwigjjp  于 2022-11-19  发布在  其他
关注(0)|答案(2)|浏览(182)

我有一个mui模型,里面有很多内容要显示。当我打开这个模型的时候,上面的内容被截断了,我无法向上滚动。我已经试着在这个模型中添加{overflow:“scroll”}属性。但是,它不起作用。这是我目前正在使用的代码:

<Modal
    open={viewCreateSegmentModal}
    onClose={() => handleCreateSegmentModal(false)}
    sx={{ 
    overflow:'scroll',
    height:'100%'}}
  >
    <div style={{overflow:"scroll"}}>
      <CreateSegmentModal
        modalControl={(value) => handleCreateSegmentModal(value)}
      />
      </div>
  </Modal>

对于如何解决此问题,有何建议?

yebdmbv4

yebdmbv41#

我也在Material UI的Modal组件中遇到了这个问题。为了添加到其他人的评论中,我在下面做了这个,它确实起到了作用。注意:我还设置了一个styles-in-js来在窗口中心显示模态。使用MUI 5.8.x w/ React。如下所示:

...
import { Modal, Box, ... } from '@mui/material';
...

...
const formStyle = {
    // These 4 below are positionings I used for larger 
    // height viewports - centered
    position: 'absolute', 
    top: '50%',
    left: '50%',
    transform: 'translate(-50%, -50%)',
    // other styles...
    width: '52%',
    maxWidth: '600px',
    minWidth: '270px',
    boxShadow: 24,
    py: 4,
    borderRadius: 3,
    zIndex: 100,
    // media query @ the max height you want (my case is the 
    // height of the viewport before the cutoff phenomenon) - 
    // set the top to '0' and translate the previous 'y' 
    // positioning coordinate so the top of the modal is @ the 
    // top of the viewport
    '@media(max-height: 890px)': {
        top: '0',
        transform: 'translate(-50%, 0%)'
    }
};

const MyStubbornModal = () => {
  ...
  return (
    ...
      // set the modal overflowY to scroll
      <Modal sx={{overflowY: 'scroll'}} disableScrollLock={false} ... >
        // I have an inner div (MUI Box), designated as a 'form' 
        // that is assigned the style above
        <Box sx={formStyle} component="form" ... >
          ...
        </Box>
      </Modal>
    ...
  );
}

export default MyStubbornModal;
ttcibm8c

ttcibm8c2#

尝试将Modal内容放在Box中而不是div中,并将溢出:滚动-类似于下面内容:

<Modal
    open={viewCreateSegmentModal}
    onClose={() => handleCreateSegmentModal(false)}
  >
        <Box className="modalBox" sx={{position: "absolute", overflowY: "scroll", maxHeight: "90%"}}>

         <CreateSegmentModal
           modalControl={(value) => handleCreateSegmentModal(value)}
         />
      </Box>
  </Modal>

相关问题