微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用Gatsby-background-image时如何在手机上使用纯背景色

如何解决使用Gatsby-background-image时如何在手机上使用纯背景色

我正在使用gatsby-background-image来模糊我网站上的全屏图像。在移动版本上,背景图像几乎看不见,因此为了加快速度,我希望那里只是背景颜色。在CSS中隐藏该组件,然后更改桌面的可见性不起作用,因为所有子级也都被隐藏了。我发现了有关Art Direction的信息,但是由于我没有设置多个背景图片,因此不确定是否应该使用它。如果有人可以帮助我,我将不胜感激:)

backgroundsection.js

import React from 'react';
import { graphql,useStaticQuery } from 'gatsby';
import BackgroundImage from 'gatsby-background-image';

const FullBackground = ({ children }) => {
  const { desktop } = useStaticQuery(
    graphql`
      query {
        desktop: file(relativePath: { eq: "background-wide.jpg" }) {
          childImageSharp {
            fluid(quality: 100,maxWidth: 3310) {
              ...GatsbyImageSharpFluid_withWebp
            }
          }
        }
      }
    `);
    
  return (
    <BackgroundImage
      Tag="section"
      fluid={desktop.childImageSharp.fluid}
      title="Fullscreen Background"
      id="fullscreenbg"
      role="img"
      aria-label="Fullscreen Background"
      preserveStackingContext={true}
      style={{

        backgroundSize: 'cover',backgroundPosition: 'center center',backgroundAttachment: 'fixed',}}
    >
      {children}
    </BackgroundImage>
  );
};

export default FullBackground;

解决方法

我最终使用 Art Direction 在移动设备上没有背景,我将默认源设置为空:

  const { desktop } = useStaticQuery(
    graphql`
      query {
        desktop: file(relativePath: { eq: "background-wide.jpg" }) {
          childImageSharp {
            fluid(quality: 100,maxWidth: 3310) {
              ...GatsbyImageSharpFluid_withWebp
            }
          }
        }
      }
    `);
  
  const sources = [
    {aspectRatio: 1,src: '',srcSet: ''},{...desktop.childImageSharp.fluid,media: `(min-width: 640px)`},]
    
  return (
    <BackgroundImage
      Tag="section"
      backgroundColor="#000" 
      fluid={sources}
      ...
    >
      {children}
    </BackgroundImage>
  );

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。