繁体   English   中英

如何在媒体查询中使用API​​返回的图像作为背景图像?

[英]How to use API returned image for background image in media query?

我的style.scss中有这个:

.myComponent {

    &__pic {

        // some other styles

        @media (max-width: 767px),
          (-webkit-min-device-pixel-ratio: 2),
          (min-resolution: 192dpi) {
          background-image: url({this needs to come from API});
        }

    }

}

我知道如果只是默认背景图像,该怎么办,如下所示:

<div className="col-md-8">
    <div className="myComponent__pic" style={{
        backgroundImage: `url(${someObject.someImage})`
    }} />
</div>

但是,如何在样式表之外指定@media样式,以便可以给它来自API的图像( someObject.someImage2x )?

您可能要使用CSS变量作为解决方法。

 let imageQuery = { --backgroundImage-2x: `url(${someObject.someImage2x})`, --backgroundImage-1x: `url(${someObject.someImage1x})` } <div className="col-md-8"> <div className="myComponent__pic" style={imageQuery} /> </div> 
 .myComponent { &__pic { --backgroundImage-2x: ""; --backgroundImage-1x: ""; // some other styles background-image: var(--backgroundImage-1x); @media (max-width: 767px), (-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi) { background-image: var(--backgroundImage-2x); } } } 

暂无
暂无

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

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