Gle*_*len 5 javascript reactjs graphql gatsby
gatsby-image 使用 gatsby-image-wrapper div 包装每个图像,该 div 填充 100% 的可用视口宽度。这个包装 div 可以很容易地用 CSS 设置样式,但没有办法以不同的方式对待横向、纵向或方形图像。
如果您想让横向图像填充可用宽度的 80%-100%,但让纵向和方形图像填充不超过视口宽度的 40-50%,该怎么办。
所以理想情况下,每个 gatsby-image-wrapper div 都会根据其纵横比添加一个类,要么是;landscape,portrait或square。
一种方法是使用 childImageSharp 附带的纵横比编写一些条件语句:
edges {
node {
name
childImageSharp {
fluid(maxWidth: 915, quality: 90) {
aspectRatio
...GatsbyImageSharpFluid_withWebp
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
当我映射了我所有的画廊图像,我可以抓住的宽高比和使用的className它添加到每个盖茨比形象的方式包装,但它不是在它的原始格式非常有用,因为对于的aspectRatio返回的数据都是相同的数字0.6666666666666666肖像图像或1.5003750937734435为风景。使用上面提到的那些类会更好;landscape,portrait或square。
这就是我如何从当前帖子中获取我的所有图库图片以及它们的aspectRatio.
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>
);
};
Run Code Online (Sandbox Code Playgroud)
我使用的完整 GraphQL 查询是:
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
}
}
}
}
}
}
`;
Run Code Online (Sandbox Code Playgroud)
必须有一个简单的解决方案,使用 React 中的条件语句来映射所有图像,获取纵横比,然后将原始数据转换为所需的类。
所以而不是:
<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>
Run Code Online (Sandbox Code Playgroud)
你会得到:
<div class="landscape gatsby-image-wrapper"></div>
<div class="portrait gatsby-image-wrapper"></div>
<div class="portrait gatsby-image-wrapper"></div>
<div class="square gatsby-image-wrapper"></div>
<div class="landscape gatsby-image-wrapper"></div>
Run Code Online (Sandbox Code Playgroud)
然后可以使用 css 轻松设置样式。
除非我遗漏了什么,否则你不是已经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>
)
}
Run Code Online (Sandbox Code Playgroud)
另请注意,当您循环某些内容时,您需要放入key最外部的组件 - 在本例中为外部<div>,而不是<Img>。
| 归档时间: |
|
| 查看次数: |
1620 次 |
| 最近记录: |