简体   繁体   English

防止内容在加载时跳到图像下方

[英]Prevent Content Jumping Below Image as it Loads

I currently have an image that I am giving 100% width.我目前有一个图像,我给出了 100% 的宽度。 The issue I am noticing is that while the image loads, the content below jumps up to where the image sits while it loads.我注意到的问题是,当图像加载时,下面的内容会在加载时跳转到图像所在的位置。 I know I need to give the image some sort of fixed height, but I want the image to remain responsive.我知道我需要给图像某种固定的高度,但我希望图像保持响应。 The desired effect: The image looks exactly as it does when applying 100% width and prevents the content below from jumping while the image loads.预期效果:图像看起来与应用 100% 宽度时完全一样,并防止在图像加载时下面的内容跳跃。 Please advise on this.请就此提出建议。 My code is as follows:我的代码如下:

<img src={"https://storage.googleapis.com/youfit-assets/239315_66400_YouFit_JUL21_Mil_Website_1680x761_CarInitial.jpg"}
  alt="travel"
  className="image"/>

Styles:款式:

.image {
  position: relative;
  width: 100%;
  height: auto;
  max-height: 100%;
}

In order to duplicate what I am seeing test on an incognito browser with the following link https://codesandbox.io/s/gifted-montalcini-ohts8?file=/src/App.js为了使用以下链接复制我在隐身浏览器上看到的测试https://codesandbox.io/s/gifted-montalcini-ohts8?file=/src/App.js

Wrap your img tag into div, so it should look like:将你的 img 标签包裹到 div 中,所以它应该看起来像:

<div className="image-wrapper">
    <img src={...} alt="travel" className="image"/>
</div>
<div style={{ marginTop: 60 }}>TEXT BELOW</div>

then replace your style for image with this style (if your aspect ratio is 4:3):然后用这种风格替换你的图像风格(如果你的纵横比是 4:3):

.image-wrapper {
    position: relative;
    padding-bottom: 37.5%;
}

.image {
    position: absolute;
    width: 100%;
}

or if your aspect ratio is 16:9或者如果您的纵横比为 16:9

.image-wrapper {
    position: relative;
    padding-bottom: 56.25%;
}

.image {
    position: absolute;
    width: 100%;
}

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

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