Answer the question
In order to leave comments, you need to log in
How to make a transition between photos in the gallery?
I'm making a gallery. Clicking on the photo opens a modal window with the image in full screen. I would like to make it possible to switch between photos using the "Left" and "Right" buttons through a modal window, I just don't understand how to do it yet.
There is a gallery component that, using map, renders an array with photos, the data about which is stored in a separate file. Here is part of the gallery component:
<div className="inner-section">
<Fade bottom>
{
photos.map(item => <GalleryPhoto
key = {item.id}
imgSrc = {item.photo}
photoId = {item.id}
/>)
}
</Fade>
<ModalImage />
</div>
const photos = [{
photo: "./images/img1.jpg",
id: 1,
caption: "",
},
{
photo: "./images/img2.jpg",
id: 2,
caption: "",
}];
export default class GalleryPhoto extends Component {
render() {
const {imgSrc, photoId} = this.props;
return (
<ProductConsumer>
{value => {
const {handleModal} = value;
return (
<div className="gallery-image">
<img onClick={() => handleModal(photoId, imgSrc)} src={imgSrc} alt="" />
</div>
)
}}
</ProductConsumer>
)
}
}
handleModal = (id, photo) => {
this.setState({
modalOpen: !this.state.modalOpen,
imageSRC: photo,
}, console.log(id, this.state.imageSRC))
}
export default class ModalImage extends Component {
render() {
return (
<ProductConsumer>
{value => {
const {modalOpen, imageSRC, handleModal} = value;
return (
<div className={"modal-container " + (modalOpen ? "visible" : '')}>
<button className="btn move-btn">Left</button>
<div className="modal-image-container">
<img src={imageSRC} alt=""/>
</div>
<button className="btn modal-close-btn" onClick={handleModal}>Close</button>
<button className="btn move-btn">Right</button>
</div>
)
}}
</ProductConsumer>
)
}
}
Answer the question
In order to leave comments, you need to log in
it is better to make a modal separately, put it in the gallery, in the gallery store the id of the current photo that you want to show in the modal in the state.
when clicking on a photo, put this id in the id of the clicked photo and show the modal, pass this id to it.
when clicking left-right, change the id to the next/previous one, the modal will be redrawn with a new id, in the modal take the desired url from the list with a photo
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question