简体   繁体   English

如何在材质 ui 上将模态与过渡居中并使其具有响应性?

[英]How to center modal with transition on material ui and make it responsive?

I trying to use modal with transition using Material UI and have problem to centering it and also make it responsive or center in small size screen (mobile).我尝试使用 Material UI 使用带有过渡的模态,但在将其居中并使其具有响应性或在小尺寸屏幕(移动)中居中时遇到问题。

Modal can be centered and looks good on small size if not using transition, but if i add a transition, modal can't be centered or responsive.如果不使用过渡,模态可以居中并且在小尺寸上看起来不错,但是如果我添加过渡,模态不能居中或响应。

This is the code modal without transition link .这是没有转换链接的代码模态。 Works good with large or small screen size.适用于大屏幕或小屏幕。

This is the code modal with transition link .这是带有转换链接的代码模式。

I tried to change the value of top & left but still can't get centered at the large and small screen size :我试图改变top & left的值,但仍然无法在大屏幕和小屏幕尺寸上居中:

function getModalStyle() {
  const top = 25;
  const left = 25;

  return {
    top: `${top}%`,
    left: `${left}%`,
    transform: `translate(-${top}%, -${left}%)`,
  };
}

Can anyone help me?谁能帮我?

By default the modal creates a parent container that uses flex, so in order to center you can comment your left: property that it is set to your modal,默认情况下,模态会创建一个使用 flex 的父容器,因此为了居中,您可以注释您的 left: 属性,将其设置为您的模态,

return {
    top: `${top}%`,
    margin:'auto'
    // left: `${left}%`,
    // transform: `translate(-${top}%, -${left}%)`,
  };

and in your modal container you can align items with this在您的模态容器中,您可以将项目与此对齐

 <Modal
    ...
    style={{display:'flex',alignItems:'center',justifyContent:'center'}}
  >

https://stackblitz.com/edit/react-1ny5g7?file=demo.js https://stackblitz.com/edit/react-1ny5g7?file=demo.js

This works for me:这对我有用:

function getModalStyle() {
  return {
    top: '50%',
    left: '50%',
    transform: 'translate(-50%, -50%)',
  };
}

This worked for me:这对我有用:

{
    width: '100%',
    maxWidth: '100vw',
    maxHeight: '100%',
    position: 'fixed',
    top: '50%',
    left: '0',
    transform: 'translate(0, -50%)',
    overflowY: 'auto'
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM