React Gatsbyjs根据宽高比将类添加到gatsby-image

时间:2019-07-01 08:56:04

标签: javascript reactjs graphql gatsby

gatsby-image用gatsby-image-wrapper div包装每个图像,该div占可用视口宽度的100%。这个包装div可以轻松地使用CSS设置样式,但是无法将风景,肖像或正方形图像彼此区别对待。

like this example如果,您希望风景图像填充可用宽度的80%-100%,而纵向和正方形图像填充不超过视口宽度的40-50%,该怎么办?

因此,理想情况下,每个gatsby-image-wrapper div都应根据其纵横比添加一个类,该纵横比可以是: landscapeportraitsquare

一种方法是使用childImageSharp随附的宽高比编写一些条件语句:

      edges {
        node {
          name
          childImageSharp {
            fluid(maxWidth: 915, quality: 90) {
              aspectRatio
              ...GatsbyImageSharpFluid_withWebp
            }
          }
        }
      }

当我映射所有画廊图像时,我可以获取长宽比,并使用className将其添加到每个gatsby-image-wrapper中,但它的原始格式不是很有用,因为AspectRatio的返回数据是{{ 1}}(用于纵向图像)或0.6666666666666666(用于横向图像)。拥有上面提到的那些课程会更好。 1.5003750937734435landscapeportrait

这就是我从当前帖子中获取所有画廊图像及其square的方式。

aspectRatio

我正在使用的完整GraphQL查询是:

export default ({ data }) => {
  return (
    <Layout>
      <article>
        {data.allFile.edges.map(({ node }, index) => (
          <div>
            <Img
              key={index}
              className={node.childImageSharp.fluid.aspectRatio}
              alt={node.name}
              fluid={node.childImageSharp.fluid}
            />
            <span>{node.childImageSharp.fluid.aspectRatio}</span>
          </div>
        ))}
      </article>
    </Layout>
  );
};

使用React中的条件语句,必须有一个简单的解决方案,您可以在其中映射所有图像,并采用宽高比 然后将原始数据转换为所需的类。

所以代替:

export const query = graphql`
  query($slug: String!, $absolutePathRegex: String!) {
    markdownRemark(fields: { slug: { eq: $slug } }) {
      html
      frontmatter {
        title
        date
        modified
        caption
        description
        cover {
          publicURL
          childImageSharp {
            fluid(maxWidth: 915, quality: 90) {
              ...GatsbyImageSharpFluid_withWebp
            }
          }
        }
      }
      fields {
        slug
      }
    }
    allFile(
      filter: {
        extension: { regex: "/(jpg)|(png)|(tif)|(tiff)|(webp)|(jpeg)/" }
        absolutePath: { regex: $absolutePathRegex }
      }
    ) {
      edges {
        node {
          name
          childImageSharp {
            fluid(maxWidth: 915, quality: 90) {
              aspectRatio
              ...GatsbyImageSharpFluid_withWebp
            }
          }
        }
      }
    }
  }
`;

您会得到:

<div class="1.5003750937734435 gatsby-image-wrapper"></div>
<div class="0.6666666666666666 gatsby-image-wrapper"></div>
<div class="0.6666666666666666 gatsby-image-wrapper"></div>
<div class="1.0000000000000000 gatsby-image-wrapper"></div>
<div class="1.5003750937734435 gatsby-image-wrapper"></div>

然后可以轻松地使用CSS设置样式。

1 个答案:

答案 0 :(得分:1)

除非我丢失了某些东西,否则您是否已经99%了?您可以在map内编写条件,或者更好的是,编写包装<Img>的组件:

import React from 'react'
import Img from 'gatsby-image'

// we only care about `aspectRatio`, the rest will be passed directly to `Img`
// also take out `className` so it be merged with our generated `orientation` class name
const ImgWithOrient = ({ aspectRatio, className, ...props }) => {
  let orientation
  if (aspectRatio > 1) orientation = 'landscape'
  if (aspectRatio < 1) orientation = 'portrait'
  else orientation = 'square'

  return <Img className={`${className} ${orientation}`} {...props} />
}

export default ({ data }) => {
  return (
    <Layout>
      <article>
        {data.allFile.edges.map(({ node }, index) => (
          <div key={index}>
            <ImgWithOrient
              key={index}
              aspectRatio={node.childImageSharp.fluid.aspectRatio}
              className="other class name"
              alt={node.name}
              fluid={node.childImageSharp.fluid}
            />
            <span>{node.childImageSharp.fluid.aspectRatio}</span>
          </div>
        ))}
      </article>
    </Layout>
  )
}

还请注意,当您遍历某些内容时,需要将key放在最外面的组件中,在这种情况下,应放在外面的<div>而不是<Img>上。