having some issues with hoverZoom getting stuck #337
-
it gets stuck when cursor on the image before it loads below is my code: import Image from "next/image";
import { useZoomImageHover } from "@zoom-image/react";
import React, { useEffect, useRef, useState } from "react";
function WebImageCarousel({ images, thumbnails, original }) {
const containerRef = useRef<HTMLDivElement>(null);
const zoomTargetRef = useRef<HTMLDivElement>(null);
const thumbnailsRef = useRef<HTMLDivElement>(null);
const { createZoomImage } = useZoomImageHover();
const [currImage, setCurrImage] = useState(images?.[0]);
const [originalImg, setOriginalImg] = useState(original?.[0]);
useEffect(() => {
setCurrImage(images?.[0]);
setOriginalImg(images?.[0]);
}, [images]);
useEffect(() => {
const zoomTarget = zoomTargetRef.current as HTMLDivElement;
if (containerRef.current) {
createZoomImage(containerRef.current, {
zoomImageSource: originalImg,
zoomTarget,
customZoom: { width: 500, height: 650 },
scale: 2,
zoomLensClass: "bg-white bg-opacity-35",
});
}
}, [originalImg, createZoomImage]);
const scrollUp = () => {
if (thumbnailsRef.current) {
thumbnailsRef.current.scrollBy({ top: -100, behavior: "smooth" });
}
};
const scrollDown = () => {
if (thumbnailsRef.current) {
thumbnailsRef.current.scrollBy({ top: 100, behavior: "smooth" });
}
};
return (
<div className="flex flex-row gap-x-4 justify-center items-start w-full scrollbar-hide">
<div className="flex flex-col h-[680px] scrollbar-none ">
<div ref={thumbnailsRef} className="flex flex-col h-full gap-3 overflow-y-auto">
{thumbnails?.map((image, key) => (
<Image
width={96}
height={120}
key={key}
src={image}
alt={`Thumbnail ${key + 1}`}
className="w-24 border object-cover object-top cursor-pointer hover:border-black transition-colors"
onClick={() => {
setCurrImage(images[key]);
setOriginalImg(images[key]);
}}
/>
))}
</div>
<div className="flex flex-row justify-end items-center space-x-4 mt-1">
<img
src={"/media/svg/blackDownArrow.svg"}
className="w-7 rotate-180"
onClick={scrollUp}
/>
<img
src={"/media/svg/blackDownArrow.svg"}
className="w-7"
onClick={scrollDown}
/>
</div>
</div>
<div ref={containerRef} className="relative flex items-start cursor-crosshair">
<img className="h-full w-full" alt="Current product image" src={currImage} />
<div ref={zoomTargetRef} className="absolute top-0 left-full ml-4"></div>
</div>
</div>
);
}
export default WebImageCarousel; |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Hi there, sorry for my late response as I have been occupied at work. I think we can fix it by giving the image container's width and height as in this example. This will solve the issue and avoid jumping experience on initial load :) |
Beta Was this translation helpful? Give feedback.
Hi there, sorry for my late response as I have been occupied at work. I think we can fix it by giving the image container's width and height as in this example. This will solve the issue and avoid jumping experience on initial load :)