I am writing a simple React project testing the lightbox effect using react-photoswipe-gallery
. I wrote the onWheel
function to implement image zoom in/out on mouse scroll. This function worked just fine for the <div>
outside the <Gallery>
tag.
However, the same code does not work for the <div>
within the <Gallery>
tag. I inspected both images inside and outside the <Gallery>
tag, while the outside ${pos}
changes on mouse scroll, the one inside the <Gallery>
remain to ne the initial value.
import { useState } from "react";
import ReactDOM from "react-dom/client";
import { Gallery, Item } from 'react-photoswipe-gallery';
import 'photoswipe/dist/photoswipe.css';
function FavoriteColor() {
const [pos, setPos] = useState(1);
const onWheel = (event) => {
const deltaY = event.deltaY;
const scaleFactor = 0.1;
setPos(prevScale => {
let newScale = prevScale;
if (deltaY < 0) {
newScale += scaleFactor;
} else {
newScale -= scaleFactor;
}
return newScale;
});
};
console.log(pos);
return (
<div>
<div onWheel={onWheel}>
<img src='https://farm4.staticflickr.com/3894/15008518202_b016d7d289_m.jpg' style={{
transformOrigin: "0 0",
transform: `scale(${pos})`
}} />
</div>
<Gallery >
<div
style={{
display: 'grid',
gridTemplateColumns: '240px 171px 171px',
gridTemplateRows: '114px 114px',
gridGap: 12,
}}
>
<Item
content={
<div onWheel={onWheel}>
<img className={pos} src='https://farm4.staticflickr.com/3894/15008518202_b016d7d289_m.jpg' style={{
transformOrigin: "0 0",
transform: `scale(${pos})`
}} />
</div>}
>
{({ ref, open }) => (
<img
style={{ cursor: 'pointer' }}
src="https://farm4.staticflickr.com/3894/15008518202_b016d7d289_m.jpg"
ref={ref}
onClick={open}
/>
)}
</Item>
</div>
</Gallery>
</div>
);
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<FavoriteColor />);
2
Answers
Replace onWheelCapture with onWheel on the div element. This will allow the scroll event to propagate to the child img element as well.