我有 16 张图像要以网格格式呈现到网站上。
我为此使用了以下插件:
gatsby-imagegatsby-source-filesystemgatsby-plugin-sharpgatsby-transformer-sharp我阅读了文档,据我所知,它只演示了如何查询单个图像。
例如
import React from "react"
import { graphql } from "gatsby"
import Img from "gatsby-image"
export default ({ data }) => (
<div>
<h1>Hello gatsby-image</h1>
<Img fixed={data.file.childImageSharp.fixed} />
</div>
)
export const query = graphql`
query {
file(relativePath: { eq: "blog/avatars/kyle-mathews.jpeg" }) {
childImageSharp {
# Specify the image processing specifications right in the query.
# Makes it trivial to update as your page's design changes.
fixed(width: 125, height: 125) {
...GatsbyImageSharpFixed
}
}
}
}
`
Run Code Online (Sandbox Code Playgroud)
但是,如果我有 16 张图像,我将如何处理?编写 16 个单独的查询似乎相当麻烦,将来会很难阅读。
我在引用多个图像的文档中看到了这段代码,但我在尝试访问图像本身时遇到了麻烦。
例如
export const squareImage = graphql`
fragment squareImage on File {
childImageSharp {
fluid(maxWidth: 200, maxHeight: 200) {
...GatsbyImageSharpFluid
}
}
}
`
export const query = graphql`
query {
image1: file(relativePath: { eq: "images/image1.jpg" }) {
...squareImage
}
image2: file(relativePath: { eq: "images/image2.jpg" }) {
...squareImage
}
image3: file(relativePath: { eq: "images/image3.jpg" }) {
...squareImage
}
}
`
Run Code Online (Sandbox Code Playgroud)
我的文件夹结构如下:
---package.json
---源代码
- - - 图片
---------16张图片
------页面
---------我想在其中渲染 16 个图像的页面
等等。
谢谢你。
ksa*_*sav 13
在 GraphiQL 中闲逛应该会对您有所帮助,尤其是 Explorer。尽管请记住 Gatsby 片段在 GraphiQL 中不起作用。
{
allImageSharp {
edges {
node {
id
fluid(maxWidth: 200, maxHeight: 200) {
...GatsbyImageSharpFluid
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
所以上面应该等于下面的查询,它将在 GraphiQL 中工作
{
allImageSharp {
edges {
node {
id
fluid(maxHeight: 200, maxWidth: 200) {
src
srcSet
base64
aspectRatio
originalImg
sizes
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后您的组件可以使用相同的查询并呈现如下结果:
import React from "react"
import { graphql } from "gatsby"
import Img from "gatsby-image"
const imgGridStyle = {
display: 'grid',
gridTemplateColumns: `repeat(auto-fill, 200px)`
};
export default ({ data }) => (
<div>
<h1>Hello gatsby-image</h1>
<div style={imgGridStyle}>
{data.allImageSharp.edges.map(edge =>
<Img fluid={edge.node.fluid} />
)}
</div>
</div>
)
export const query = graphql`
query {
allImageSharp {
edges {
node {
id
fluid(maxWidth: 200, maxHeight: 200) {
...GatsbyImageSharpFluid
}
}
}
}
}
`
Run Code Online (Sandbox Code Playgroud)
您可以轻松地遍历从data.allImageSharp.edges.map. 然后传递每个节点的fluid属性,作为fluidprop 传递给gatsby-image.
注意:这会渲染您项目中的每个imageSharp 节点,这可能是您想要实现的,也可能不是。
要按文件夹名称过滤查询,您可以像这样调整查询:
{
allImageSharp(filter: {fileAbsolutePath: {regex: "/(myFolder)/" }}) {
edges {
node {
id
fluid(maxWidth: 200, maxHeight: 200) {
...GatsbyImageSharpFluid
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
查看过滤器的gatsby graphql 参考,了解如何对查询执行其他类型的过滤器。
从 Gatsby v3 开始,您只需要使用gatsby-plugin-image插件,它提供自动图像优化
import { StaticImage } from "gatsby-plugin-image"
export function Dino() {
return (
<StaticImage
src="../images/dino.png"
alt="A dinosaur"
placeholder="blurred"
layout="fixed"
width={200}
height={200}
/>
)
}
Run Code Online (Sandbox Code Playgroud)
更多信息:https : //www.gatsbyjs.com/docs/how-to/images-and-media/using-gatsby-plugin-image
盖茨比 v2:
最简单的方法是创建一个图像提供程序:
import React from 'react'
import { graphql, useStaticQuery } from 'gatsby'
import Img from 'gatsby-image'
const Image = ({ fileName, alt, style }) => {
const { allImageSharp } = useStaticQuery(graphql`
query {
allImageSharp {
nodes {
fluid(maxWidth: 1600) {
originalName
...GatsbyImageSharpFluid_withWebp
}
}
}
}
`)
const fluid = allImageSharp.nodes.find(n => n.fluid.originalName === fileName)
.fluid
return (
<figure>
<Img fluid={fluid} alt={alt} style={style} />
</figure>
)
}
export default Image;
Run Code Online (Sandbox Code Playgroud)
然后,导入后,轻松插入您需要的图像:
<Image fileName="yourImage.jpg" style={{ width: '100%' }} alt="" />
Run Code Online (Sandbox Code Playgroud)
尽管当前提出的问题中存在这种情况,但所有 16 个图像都位于图像文件夹内,因此只需运行查询即可轻松获取所有可能的图像。像这样(接受的答案):
{
allImageSharp {
edges {
node {
id
fluid(maxWidth: 200, maxHeight: 200) {
...GatsbyImageSharpFluid
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
但在大多数情况下,您希望在图像文件夹内有子文件夹,以便根据要求排列图像。(至少那是我的情况)。
所以在这种情况下:(如果子文件夹中有图像,假设图像中有海滩,您可以遵循这种方法)
export const query = graphql`
query {
allFile(filter: { relativeDirectory: { eq: "beach" } }) {
edges {
node {
id
childImageSharp {
fluid {
...GatsbyImageSharpFluid_withWebp
}
}
}
}
}
}
`
Run Code Online (Sandbox Code Playgroud)
如果您想在视频中观看,这是一个小知识分子剪辑。
| 归档时间: |
|
| 查看次数: |
11387 次 |
| 最近记录: |