【发布时间】:2023-04-06 20:40:02
【问题描述】:
我目前正在通过我的 Gatsby 网站中的一组图像进行映射,其中每张图像都有一个 395 像素的固定高度和自动宽度。但是,对于 768 像素以下的屏幕尺寸,我需要能够将图像的固定高度更改为 200 像素。
我尝试使用两个单独的图像组件来执行此操作,并使用 windowSize 条件渲染它们,但这并没有真正起作用。
我怎样才能实现上述目标?下面是我的固定高度为 395px 的图像组件。
ScrollImage.tsx
const ScrollImage = ({ src, ...rest }) => {
const data = useStaticQuery(graphql`
query {
images: allFile(
filter: { internal: { mediaType: { regex: "/image/" } } }
) {
edges {
node {
relativePath
extension
publicURL
childImageSharp {
fixed(height: 395) {
...GatsbyImageSharpFixed
}
}
}
}
}
}
`);
const match = useMemo(
() => data.images.edges.find(({ node }) => src === node.relativePath),
[data, src]
);
if (!match) return null;
const { node: { childImageSharp, publicURL, extension } = {} } = match;
if (extension === "svg" || !childImageSharp) {
return <img src={publicURL} {...rest} />;
}
return (
<Img
fixed={childImageSharp.fixed}
imgStyle={{ WebkitUserDrag: "none" }}
{...rest}
/>
);
};
export default ScrollImage;
【问题讨论】:
标签: javascript reactjs gatsby gatsby-image