Gatsby 插件图像 - 图像滑块无法正常加载
Gatsby Plugin Image - Eager loading not functioning with image slider
我正在尝试使用 Gatsby JS (SSG) 和 framer motion 构建图像滑块。我 运行 遇到一个问题,即页面上不可见的图像(除了选项卡 1 之外的所有选项卡)没有被预渲染,尽管 GatsbyImage 提供了 loading="eager" 属性。
我已经构建了一个图像提供程序来查询图像滑块中的图像:
const ImageProvider = ({ fileName, alt, imgStyle, style, loading, placeholder }) => {
const { allImageSharp } = useStaticQuery(graphql`
{
allImageSharp {
nodes {
parent {
... on File {
base
}
}
gatsbyImageData
}
}
}
`)
const image = allImageSharp.nodes.find(node => node.parent.base === fileName).gatsbyImageData;
if (!image) return null;
return (
<GatsbyImage image={image} alt={alt} imgStyle={imgStyle} style={style} loading={loading} placeholder={placeholder} />
)
}
export default ImageProvider;
这是图片滑块:
export const TabSlider = ({
tabs,
}) => {
const [[page, direction], setPage] = useState([0, 0]);
const elementRef = useRef(null);
const elementSize = useElementSize(elementRef);
// We only have 3 images, but we paginate them absolutely (ie 1, 2, 3, 4, 5...) and
// then wrap that within 0-2 to find our image ID in the array below. By passing an
// absolute page index as the `motion` component's `key` prop, `AnimatePresence` will
// detect it as an entirely new image. So you can infinitely paginate as few as 1 images.
// const imageIndex = wrap(0, images.length, page);
const paginate = (newDirection) => {
setPage([page + newDirection, newDirection]);
};
return (
<Container>
<ImageProvider style={{ width: "100%" }} alt="" loading="eager" placeholder="none" />
<AnimateSharedLayout>
<TabsContainer>
<TabsHeader>
{tabs.map(({ title }, i) => {
const isActive = i === page;
return (
<TabItem
key={i}
className={isActive ? "active-header" : ""}
onClick={() => {
// set page and determine which direction we're going
setPage([i, i - page]);
}}
>
<TabHeaderContainer>
<TabHeader>{title}</TabHeader>
</TabHeaderContainer>
{isActive && (
<Underline as={motion.div} layoutId="underline" />
)}
</TabItem>
);
})}
<UnderlineBg />
</TabsHeader>
</TabsContainer>
<ContentContainer>
<AnimatePresence initial={false} custom={direction}>
<Section
as={motion.section}
ref={elementRef}
key={page}
custom={direction}
variants={variants}
initial="enter"
animate="center"
exit="exit"
transition={{
x: { type: "spring", stiffness: 300, damping: 30, duration: 2 },
opacity: { duration: 0.2 }
}}
drag="x"
dragConstraints={{ left: 0, right: 0 }}
dragElastic={1}
onDragEnd={(e, { offset, velocity }) => {
const swipe = swipePower(offset.x, velocity.x);
if (swipe < -swipeConfidenceThreshold) {
paginate(1);
} else if (swipe > swipeConfidenceThreshold) {
paginate(-1);
}
}}
>
<Background height={elementSize.height} />
<Wrapper>
<SlideContainer ref={elementRef}>
<CopywritingContainer>
<H2Header>{tabs[page].header}</H2Header>
<Text>{tabs[page].text}</Text>
</CopywritingContainer>
<ImageContainer>
<ImageProvider fileName={tabs[page].imageFilename} style={{ width: "50%" }} alt="" loading="eager" placeholder="none" />
</ImageContainer>
</SlideContainer>
</Wrapper>
</Section>
</AnimatePresence>
</ContentContainer>
</AnimateSharedLayout>
</Container>
);
};
我看不出哪里出了问题,非常感谢任何指导。
谢谢!
目前似乎无法使用 gatsby-plugin-image。通过使用传统的 HTML 图片标签实现。
我正在尝试使用 Gatsby JS (SSG) 和 framer motion 构建图像滑块。我 运行 遇到一个问题,即页面上不可见的图像(除了选项卡 1 之外的所有选项卡)没有被预渲染,尽管 GatsbyImage 提供了 loading="eager" 属性。
我已经构建了一个图像提供程序来查询图像滑块中的图像:
const ImageProvider = ({ fileName, alt, imgStyle, style, loading, placeholder }) => {
const { allImageSharp } = useStaticQuery(graphql`
{
allImageSharp {
nodes {
parent {
... on File {
base
}
}
gatsbyImageData
}
}
}
`)
const image = allImageSharp.nodes.find(node => node.parent.base === fileName).gatsbyImageData;
if (!image) return null;
return (
<GatsbyImage image={image} alt={alt} imgStyle={imgStyle} style={style} loading={loading} placeholder={placeholder} />
)
}
export default ImageProvider;
这是图片滑块:
export const TabSlider = ({
tabs,
}) => {
const [[page, direction], setPage] = useState([0, 0]);
const elementRef = useRef(null);
const elementSize = useElementSize(elementRef);
// We only have 3 images, but we paginate them absolutely (ie 1, 2, 3, 4, 5...) and
// then wrap that within 0-2 to find our image ID in the array below. By passing an
// absolute page index as the `motion` component's `key` prop, `AnimatePresence` will
// detect it as an entirely new image. So you can infinitely paginate as few as 1 images.
// const imageIndex = wrap(0, images.length, page);
const paginate = (newDirection) => {
setPage([page + newDirection, newDirection]);
};
return (
<Container>
<ImageProvider style={{ width: "100%" }} alt="" loading="eager" placeholder="none" />
<AnimateSharedLayout>
<TabsContainer>
<TabsHeader>
{tabs.map(({ title }, i) => {
const isActive = i === page;
return (
<TabItem
key={i}
className={isActive ? "active-header" : ""}
onClick={() => {
// set page and determine which direction we're going
setPage([i, i - page]);
}}
>
<TabHeaderContainer>
<TabHeader>{title}</TabHeader>
</TabHeaderContainer>
{isActive && (
<Underline as={motion.div} layoutId="underline" />
)}
</TabItem>
);
})}
<UnderlineBg />
</TabsHeader>
</TabsContainer>
<ContentContainer>
<AnimatePresence initial={false} custom={direction}>
<Section
as={motion.section}
ref={elementRef}
key={page}
custom={direction}
variants={variants}
initial="enter"
animate="center"
exit="exit"
transition={{
x: { type: "spring", stiffness: 300, damping: 30, duration: 2 },
opacity: { duration: 0.2 }
}}
drag="x"
dragConstraints={{ left: 0, right: 0 }}
dragElastic={1}
onDragEnd={(e, { offset, velocity }) => {
const swipe = swipePower(offset.x, velocity.x);
if (swipe < -swipeConfidenceThreshold) {
paginate(1);
} else if (swipe > swipeConfidenceThreshold) {
paginate(-1);
}
}}
>
<Background height={elementSize.height} />
<Wrapper>
<SlideContainer ref={elementRef}>
<CopywritingContainer>
<H2Header>{tabs[page].header}</H2Header>
<Text>{tabs[page].text}</Text>
</CopywritingContainer>
<ImageContainer>
<ImageProvider fileName={tabs[page].imageFilename} style={{ width: "50%" }} alt="" loading="eager" placeholder="none" />
</ImageContainer>
</SlideContainer>
</Wrapper>
</Section>
</AnimatePresence>
</ContentContainer>
</AnimateSharedLayout>
</Container>
);
};
我看不出哪里出了问题,非常感谢任何指导。
谢谢!
目前似乎无法使用 gatsby-plugin-image。通过使用传统的 HTML 图片标签实现。