【问题标题】:gatsby-background-image crop left side of background image on browser resizegatsby-background-image 在浏览器调整大小时裁剪背景图像的左侧
【发布时间】:2020-11-29 13:52:30
【问题描述】:

我可以使用下面的代码在 div 上使用常规 css 创建我想要的结果。图片贴在屏幕右侧,图片左侧裁剪,因此当您缩小屏幕尺寸时,您仍然可以看到图片中的城堡。

#bgImg{
  background-image: url("https://i.stack.imgur.com/pfepb.jpg");
  height: 225px;
  width: 100%;
  background-position: right center;
  object-fit: fill;
}
<div id='bgImg'><div>

我无法使用gatsby-background-image 创建相同的结果。图像始终裁剪图像的右侧。

我尝试创建code sandbox,但不幸的是图像没有出现,我不知道为什么


这是我的代码,它裁剪图像的右侧而不是左侧

const bgImgProps: FluidObject = data.bessImg.childImageSharp.fluid
const otherImgProps: FluidObject = data.logoImg.childImageSharp.fluid

<BackgroundImage
        Tag="section"
        role="img"
        className='w-100 h-100'
        style={{
          backgroundPosition: "right center",
          objectFit: "fill"
        }}
        fluid={bessImgProps}
        backgroundColor={`#040e18`}
      >
         <div className='header-outer-container d-flex justify-content-center'> 
            <a className='only-gt-md' style={{height: "130px", width: "350px"}} href="/">
              <Img 
                className='w-100 h-100'
                fluid={logoImgProps}
                fadeIn={false}
                loading="eager"
                imgStyle={{objectFit: 'contain' }}
              ></Img>
            </a>
        </div>
      </BackgroundImage>
.header-outer-container{
    height: 225px;
}

header{
    min-height: 225px;
    background-position: right center;
}

这就是它的样子,但我希望从另一侧裁剪图像,以便城堡保持可见,就像上面的普通 css 示例一样。

【问题讨论】:

    标签: javascript css reactjs gatsby gatsby-image


    【解决方案1】:

    我必须将 .headerBG 类赋予我的背景图像并创建看起来像这样的 css

    .headerBG::before{
        background-position: right center;
    }
    

    然后我不需要任何其他声明 background-position: right center; 的行,从而导致 JS 和 CSS 看起来像

    const bgImgProps: FluidObject = data.bessImg.childImageSharp.fluid
    const otherImgProps: FluidObject = data.logoImg.childImageSharp.fluid
    
    <BackgroundImage
            Tag="section"
            role="img"
            className='w-100 h-100'
            style={{
              objectFit: "fill"
            }}
            fluid={bessImgProps}
            backgroundColor={`#040e18`}
          >
             <div className='header-outer-container d-flex justify-content-center'> 
                <a className='only-gt-md' style={{height: "130px", width: "350px"}} href="/">
                  <Img 
                    className='w-100 h-100'
                    fluid={logoImgProps}
                    fadeIn={false}
                    loading="eager"
                    imgStyle={{objectFit: 'contain' }}
                  ></Img>
                </a>
            </div>
          </BackgroundImage>
    .header-outer-container{
        height: 225px;
    }
    
    header{
        min-height: 225px;
    }
    
    .headerBG::before{
        background-position: right center;
    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-01
      • 2013-04-19
      • 1970-01-01
      • 2016-07-03
      • 2020-09-04
      • 1970-01-01
      • 2011-09-17
      • 2011-11-20
      相关资源
      最近更新 更多