【发布时间】:2020-06-25 07:25:28
【问题描述】:
我想有条件地渲染我的gatsby-image:我想为移动设备和桌面设备提供不同的图像。所以我需要把它们换掉。
现在我正在这样做:
<Desktop>
{heroImage && (
<MyGatsbyImage
img={heroImage}
/>
)}
</Desktop>
<Mobile>
{heroImageXS && (
<MyGatsbyImage
img={heroImageXS}
/>
)}
</Mobile>
其中<Desktop> 和<Mobile> 是带有display: block / display:none 的媒体查询的样式化组件,具体取决于视口。
但是:这是最有效的解决方案吗?我的解决方案是否总是在后台加载两张图片?
没有gatsby-image,我会这样做:
<picture>
<source
media="(min-width: 650px)"
srcset="images/img1.png">
<source
media="(min-width: 465px)"
srcset="images/img2.png">
<img src="images/img-default.png"
alt="a cute kitten">
</picture>
...但这意味着不要在这里使用gatsby-image - 我确实想使用它。
【问题讨论】:
标签: html css reactjs responsive-design gatsby-image