简体   繁体   English

由于图像加载,Material UI Grid 项目向上推

[英]Material UI Grid item pushed upward due to image loading

When I am using the Material UI grid system with a project I am doing using create-react-app.当我在一个项目中使用 Material UI 网格系统时,我正在使用 create-react-app。 I have two grid items that are adjacent to each other.我有两个彼此相邻的网格项目。 That look like so:看起来像这样:

在此处输入图像描述

This is exactly what I want.这正是我想要的。 For some reason;由于某些原因; however, when the page loads for a split second the text on the right side gets pushed upward and flashes unstyled like so:但是,当页面加载一瞬间,右侧的文本会被向上推并闪烁,如下所示: 在此处输入图像描述 I suspect it is because the image hasn't fully loaded, so the text sits higher up until the image loads.我怀疑这是因为图像尚未完全加载,因此文本位于较高位置,直到图像加载。 Any advice on how to prevent this or suggestions would be greatly appreciated!!任何有关如何防止这种情况的建议或建议将不胜感激!!

here is a code sandbox to recreate https://codesandbox.io/s/nifty-jang-kbbty?file=/src/pages/Home.js这是一个代码沙箱,用于重新创建https://codesandbox.io/s/nifty-jang-kbbty?file=/src/pages/Home.js

if you go from the home page to /portfolio when the code sandbox is in full screen, you will see the flash of unstyled content i am talking about.如果您在代码沙箱全屏时从主页 go 到 /portfolio,您将看到我正在谈论的无样式内容的 flash。

      <Grid container justify='center' alignItems='center'>
        <Grid item xs={12} sm={9} md={9} lg={6} xl={6}>
          <img src={JobTracker} alt='jobtracker' style={{ width: '100%' }} />
        </Grid>

        <Grid item xs={12} sm={9} md={9} lg={6} xl={6}>
          <Container maxWidth='xs'>
            <Typography variant='h3' style={{ textAlign: 'center' }}>
              JobTracker
            </Typography>
            <br />
            <Typography variant='body2'>
              A platform that allows recent graduates from Wyncode Academy to
              track job applications. Technologies used: ReactJS, NodeJS, Google
              Cloud Functions, and Google Firestore.
            </Typography>
         </Container>
        </Grid>
      </Grid>

In my experience, I usually wrap images in a wrapper has fixed ratio (usually golden ratio for me) to prevent this behavior.根据我的经验,我通常将图像包装在具有固定比例(对我来说通常是黄金比例)的包装器中,以防止这种行为。 This is because, as long as image has not been loaded, the image container (in your case is Grid component) has zero height.这是因为,只要图像尚未加载,图像容器(在您的情况下是Grid组件)的高度为零。

If you use fixed ratio wrapper approach, the image wrapper always have a height.如果您使用固定比率包装器方法,则图像包装器始终具有高度。 That's it.而已。 You can read more about that technique here: https://css-tricks.com/aspect-ratio-boxes/您可以在此处阅读有关该技术的更多信息: https://css-tricks.com/aspect-ratio-boxes/

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

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